1)
//since list elements are distinct
//step1: first we will find the kth largest item using kth order
statistic algorithm
int partition(int A[], int low, int high)
{
if(low==high)
return low;
if(low>high)
return -1;
int pos=high+1,j;
for(j=low+1;j<=high;j++)
{
if(A[j]>=A[low] && pos!=high+1)
{
swap(A[j],A[pos]);
pos++;
}
else if(pos==high+1 && A[j]>A[low])
pos=j;
}
pos--;
swap(A[low],
A[pos]);
return pos;
}
//finds the kth smallest item
int order_statistic(int A[], int low, int high,
int k)
{
if(low>high ||
(high-low+1)<k)
return
-1;
int
pivot=rand()%(high-low+1)+low, position, p;
swap(A[pivot],
A[low]);
position=partition(A,
low, high);
p=position;
position=position-low+1;
if(k==position)
return A[p];
else
if(k<position)
return order_statistic(A, low,p-1,k);
else
return order_statistic(A,p+1,high,k-position);
}
//step2: after finding the kth largest item, now we will
traverse the array to find the remaining largest items//those are
greater than kth largest item
//complexity: O(n)
b)
//since list elements are distinct
//step1: first we will find the kth smallest item using kth order
statistic algorithm
int partition(int A[], int low, int high)
{
if(low==high)
return low;
if(low>high)
return -1;
int pos=high+1,j;
for(j=low+1;j<=high;j++)
{
if(A[j]<=A[low] && pos!=high+1)
{
swap(A[j],A[pos]);
pos++;
}
else if(pos==high+1 && A[j]>A[low])
pos=j;
}
pos--;
swap(A[low],
A[pos]);
return pos;
}
//finds the kth smallest item
int order_statistic(int A[], int low, int high,
int k)
{
if(low>high ||
(high-low+1)<k)
return
-1;
int
pivot=rand()%(high-low+1)+low, position, p;
swap(A[pivot],
A[low]);
position=partition(A,
low, high);
p=position;
position=position-low+1;
if(k==position)
return A[p];
else
if(k<position)
return order_statistic(A, low,p-1,k);
else
return order_statistic(A,p+1,high,k-position);
}
//step2: after finding the kth smallest item, now we will
traverse the array to find the remaining smallest items//those are
less than kth small item
//complexity: O(n)
1. a) Describe an O(m)-time algorithm that, given a set of S of n distinct numbers...
1. Let m be a nonnegative integer, and n a positive integer. Using the division algorithm we can write m=qn+r, with 0 <r<n-1. As in class define (m,n) = {mc+ny: I,Y E Z} and S(..r) = {nu+ru: UV E Z}. Prove that (m,n) = S(n,r). (Remark: If we add to the definition of ged that gedan, 0) = god(0, n) = n, then this proves that ged(m, n) = ged(n,r). This result leads to a fast algorithm for computing ged(m,...
You are given a set of integer numbers A = {a1, a2, ..., an}, where 1 ≤ ai ≤ m for all 1 ≤ i ≤ n and for a given positive integer m. Give an algorithm which determines whether you can represent a given positive integer k ≤ nm as a sum of some numbers from A, if each number from A can be used at most once. Your algorithm must have O(nk) time complexity.
Problem 1 (**) Find a positive integer m such that φ(m) < 0.1m.
1. Suppose N is a set with n elements and M is a set with m elements. a. If n <m, how many one-to-one functions are there from N to M? b. If n > m, how many onto functions are there from N to M?
FOR ALGORITHM A WORST CASE TIME COMPLEXITY IS DESCRIBED BY RECURRENCE FORMULA T(n)= n/ T (n )thi T (c)=1 if c < 100 FOR ALGORITHM B WORST TIME COMPLEXITY IS DESCRIBED BY RECURRENCE FORMULA T(n) = 2T (2/2) + n/logn ; (c) = 1 fc 2100 WHICH ALGORITHM IS ASYMPTOTICALLY FASTER? WHY?
• Implement the algorithm Build Heap in C (or C++). Algorithm buildHeap (heap, size) set walker to 1 loop (walker < size) reheapUp (heap, walker) increment walker end loop • Write a program to test your program using a random integer array of size 30. Print the arrays before and after building the heap.
We write R+ for the set of positive real numbers. For any positive real number e, we write (-6, 6) = {x a real number : -e < x <e}. Prove that the intersection of all such intervals is the set containing zero, n (-e, e) = {0} EER+
Using the pseudocode answer these questions Algorithm 1 CS317FinalAlgorithm (A[O..n-1]) ito while i<n - 2 do if A[i]A[i+1] > A[i+2) then return i it i+1 return -1 1. Describe what it does and compute what value is returned when the input is the list {1, 2, 3, 4, 5}. (Hint: We're using 0-based array indexing, so 0 would represent the index of the first element, 1 the second element, etc.) 2. Identify and describe the worst-case input. 3. Identify and...
Using the pseudocode answer these questions Algorithm 1 CS317FinalAlgorithm (A[O..n-1]) ito while i<n - 2 do if A[i]A[i+1] > A[i+2) then return i it i+1 return -1 4. Calculate how many times the comparison A[i]A[i+1] > A[i+2] is done for a worst-case input of size n. Show your work. 5. Calculate how many times the comparison A[i]A[i+1] > A[i+2] is done for a best-case input of size n. Show your work.
QUESTION 3 Given X ~N(10, 18) and n=64 P(13<x< 14) =