Question

In: Computer Science

DIVIDE AND CONQUER Implement the two algorithms for finding the kth smallest integer in a set...

DIVIDE AND CONQUER

Implement the two algorithms for finding the kth smallest integer in a set of integers using only one array that contains all the integers. Test your programs and compute the CPU time for different sets of integers generated by a random number generator.

Must sure have the CPU time.

Solutions

Expert Solution

#include<iostream>
#include <cstdlib>
#include<time.h>
using namespace std;
void swap(int * a, int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}

int brootforce(int a[],int n,int k)
{
int i,j,min;
int size=n;
for(j=1;j<=k;j++,n--)
{
min=0;
for(i=1;i<n;i++)
{
if(a[min]>a[i])
{
min=i;
}
}
swap(&a[n-1],&a[min]);
}
return a[size-k];
}
int partition(int a[],int l,int r)
{
int i,j;
for(i=l,j=l;j<=r;j++)
{
if(a[j]<a[r])
{
swap(&a[i],&a[j]);
i++;
}
}
swap(&a[i],&a[r]);
return i;
}
int DAC(int a[],int l,int r,int k)
{
if(k>0 && k<=r-l+1)
{
int pivot=partition(a,l,r);
if(pivot-l==k-1)
return a[pivot];
if(pivot-l>k-1)
return DAC(a,l,pivot-1,k);
return DAC(a,pivot+1,r,k-pivot-1+l);
}
return -1;
}

int main()
{
int i=100;
int a[10000];
double time_taken;
clock_t t;
for(int i=0;i<10000;i++)
{
a[i]=i+1;
}
srand(time(0));
int r;
while(i--)
{
cout<<"Iteration: "<<i+1<<endl;
int max=10000;
for(int i=0;i<10000;i++,max--)
{
r=rand()%max;
swap(&a[r],&a[max-1]);
}
r=rand()%10000+1;
t = clock();
brootforce(a,10000,r);
t = clock() - t;
time_taken = ((double)t)/CLOCKS_PER_SEC;
cout<<"brootforce() took "<<time_taken<<"seconds\n";

t = clock();
DAC(a,0,9999,r);
t = clock() - t;
time_taken = ((double)t)/CLOCKS_PER_SEC;
cout<<"DAC() took "<<time_taken<<"seconds\n";
}
}

teration: 100
brootforce() took 0.137165seconds
DAC() took 0.000926seconds
Iteration: 99
brootforce() took 0.11362seconds
DAC() took 0.000716seconds
Iteration: 98
brootforce() took 0.058846seconds
DAC() took 0.000565seconds
Iteration: 97
brootforce() took 0.040972seconds
DAC() took 0.000329seconds
Iteration: 96
brootforce() took 0.040914seconds
DAC() took 0.000178seconds
Iteration: 95
brootforce() took 0.121494seconds
DAC() took 0.000888seconds
Iteration: 94
brootforce() took 0.069038seconds
DAC() took 0.00056seconds
Iteration: 93
brootforce() took 0.039016seconds
DAC() took 0.000195seconds
Iteration: 92
brootforce() took 0.068721seconds
DAC() took 0.000616seconds
Iteration: 91
brootforce() took 0.051939seconds
DAC() took 0.000416seconds
Iteration: 90
brootforce() took 0.131203seconds
DAC() took 0.000744seconds
Iteration: 89
brootforce() took 0.135701seconds
DAC() took 0.000521seconds
Iteration: 88
brootforce() took 0.090045seconds
DAC() took 0.000555seconds
Iteration: 87
brootforce() took 0.129142seconds
DAC() took 0.000768seconds
Iteration: 86
brootforce() took 0.128727seconds
DAC() took 0.001021seconds
Iteration: 85
brootforce() took 0.129583seconds
DAC() took 0.000815seconds
Iteration: 84
brootforce() took 0.083905seconds
DAC() took 0.000596seconds
Iteration: 83
brootforce() took 0.023002seconds
DAC() took 0.000523seconds
Iteration: 82
brootforce() took 0.031218seconds
DAC() took 0.000331seconds
Iteration: 81
brootforce() took 0.069087seconds
DAC() took 0.000494seconds
Iteration: 80
brootforce() took 0.101371seconds
DAC() took 0.00071seconds
Iteration: 79
brootforce() took 0.087531seconds
DAC() took 0.000544seconds
Iteration: 78
brootforce() took 0.108041seconds
DAC() took 0.000633seconds
Iteration: 77
brootforce() took 0.000266seconds
DAC() took 0.000198seconds
Iteration: 76
brootforce() took 0.030646seconds
DAC() took 0.000391seconds
Iteration: 75
brootforce() took 0.055807seconds
DAC() took 0.000622seconds
Iteration: 74
brootforce() took 0.129295seconds
DAC() took 0.000806seconds
Iteration: 73
brootforce() took 0.12317seconds
DAC() took 0.000726seconds
Iteration: 72
brootforce() took 0.058533seconds
DAC() took 0.000485seconds
Iteration: 71
brootforce() took 0.117206seconds
DAC() took 0.000869seconds
Iteration: 70
brootforce() took 0.048142seconds
DAC() took 0.000379seconds
Iteration: 69
brootforce() took 0.132389seconds
DAC() took 0.000847seconds
Iteration: 68
brootforce() took 0.127705seconds
DAC() took 0.000603seconds
Iteration: 67
brootforce() took 0.119727seconds
DAC() took 0.000573seconds
Iteration: 66
brootforce() took 0.004231seconds
DAC() took 0.000285seconds
Iteration: 65
brootforce() took 0.061037seconds
DAC() took 0.00041seconds
Iteration: 64
brootforce() took 0.095829seconds
DAC() took 0.000504seconds
Iteration: 63
brootforce() took 0.038883seconds
DAC() took 0.00041seconds
Iteration: 62
brootforce() took 0.033846seconds
DAC() took 0.00041seconds
Iteration: 61
brootforce() took 0.01941seconds
DAC() took 0.000222seconds
Iteration: 60
brootforce() took 0.131999seconds
DAC() took 0.000557seconds
Iteration: 59
brootforce() took 0.026399seconds
DAC() took 0.000184seconds
Iteration: 58
brootforce() took 0.042748seconds
DAC() took 0.000603seconds
Iteration: 57
brootforce() took 0.013628seconds
DAC() took 0.000292seconds
Iteration: 56
brootforce() took 0.131902seconds
DAC() took 0.000848seconds
Iteration: 55
brootforce() took 0.071388seconds
DAC() took 0.000274seconds
Iteration: 54
brootforce() took 0.074949seconds
DAC() took 0.000367seconds
Iteration: 53
brootforce() took 0.130213seconds
DAC() took 0.000957seconds
Iteration: 52
brootforce() took 0.133445seconds
DAC() took 0.000768seconds
Iteration: 51
brootforce() took 0.046403seconds
DAC() took 0.000533seconds
Iteration: 50
brootforce() took 0.068135seconds
DAC() took 0.000607seconds
Iteration: 49
brootforce() took 0.125371seconds
DAC() took 0.001031seconds
Iteration: 48
brootforce() took 0.112279seconds
DAC() took 0.000602seconds
Iteration: 47
brootforce() took 0.131578seconds
DAC() took 0.000377seconds
Iteration: 46
brootforce() took 0.070036seconds
DAC() took 0.000496seconds
Iteration: 45
brootforce() took 0.100934seconds
DAC() took 0.000651seconds
Iteration: 44
brootforce() took 0.125196seconds
DAC() took 0.001126seconds
Iteration: 43
brootforce() took 0.01428seconds
DAC() took 0.000394seconds
Iteration: 42
brootforce() took 0.113824seconds
DAC() took 0.000609seconds
Iteration: 41
brootforce() took 0.101546seconds
DAC() took 0.000714seconds
Iteration: 40
brootforce() took 0.00098seconds
DAC() took 0.000266seconds
Iteration: 39
brootforce() took 0.058767seconds
DAC() took 0.000431seconds
Iteration: 38
brootforce() took 0.09437seconds
DAC() took 0.000878seconds
Iteration: 37
brootforce() took 0.08223seconds
DAC() took 0.00047seconds
Iteration: 36
brootforce() took 0.111064seconds
DAC() took 0.000956seconds
Iteration: 35
brootforce() took 0.129108seconds
DAC() took 0.000778seconds
Iteration: 34
brootforce() took 0.133223seconds
DAC() took 0.000769seconds
Iteration: 33
brootforce() took 0.091501seconds
DAC() took 0.000689seconds
Iteration: 32
brootforce() took 0.117807seconds
DAC() took 0.000728seconds
Iteration: 31
brootforce() took 0.132821seconds
DAC() took 0.000581seconds
Iteration: 30
brootforce() took 0.138981seconds
DAC() took 0.00088seconds
Iteration: 29
brootforce() took 0.088637seconds
DAC() took 0.00077seconds
Iteration: 28
brootforce() took 0.041447seconds
DAC() took 0.000379seconds
Iteration: 27
brootforce() took 0.126821seconds
DAC() took 0.000598seconds
Iteration: 26
brootforce() took 0.110885seconds
DAC() took 0.000513seconds
Iteration: 25
brootforce() took 0.122493seconds
DAC() took 0.001145seconds
Iteration: 24
brootforce() took 0.117035seconds
DAC() took 0.000953seconds
Iteration: 23
brootforce() took 0.018786seconds
DAC() took 0.000186seconds
Iteration: 22
brootforce() took 0.000292seconds
DAC() took 0.000288seconds
Iteration: 21
brootforce() took 0.107934seconds
DAC() took 0.000963seconds
Iteration: 20
brootforce() took 0.114664seconds
DAC() took 0.000804seconds
Iteration: 19
brootforce() took 0.011936seconds
DAC() took 0.000131seconds
Iteration: 18
brootforce() took 0.000664seconds
DAC() took 0.000285seconds
Iteration: 17
brootforce() took 0.129946seconds
DAC() took 0.000629seconds
Iteration: 16
brootforce() took 0.037369seconds
DAC() took 0.000374seconds
Iteration: 15
brootforce() took 0.03226seconds
DAC() took 0.000383seconds
Iteration: 14
brootforce() took 0.037177seconds
DAC() took 0.000278seconds
Iteration: 13
brootforce() took 0.081285seconds
DAC() took 0.000508seconds
Iteration: 12
brootforce() took 0.098688seconds
DAC() took 0.0011seconds
Iteration: 11
brootforce() took 0.000134seconds
DAC() took 0.000388seconds
Iteration: 10
brootforce() took 0.126052seconds
DAC() took 0.000941seconds
Iteration: 9
brootforce() took 0.037972seconds
DAC() took 0.000452seconds
Iteration: 8
brootforce() took 0.085607seconds
DAC() took 0.000445seconds
Iteration: 7
brootforce() took 0.0409seconds
DAC() took 0.00027seconds
Iteration: 6
brootforce() took 0.121944seconds
DAC() took 0.000803seconds
Iteration: 5
brootforce() took 0.067007seconds
DAC() took 0.000558seconds
Iteration: 4
brootforce() took 0.108865seconds
DAC() took 0.000616seconds
Iteration: 3
brootforce() took 0.110891seconds
DAC() took 0.000658seconds
Iteration: 2
brootforce() took 0.033118seconds
DAC() took 0.000363seconds
Iteration: 1
brootforce() took 0.121802seconds
DAC() took 0.000654seconds


Related Solutions

Use quickselect algorithm to implement the two algorithms for finding the kth smallest integer in a...
Use quickselect algorithm to implement the two algorithms for finding the kth smallest integer in a set of integers. MUST USE JAVA AND QUICKSELECT. Algorithm 1: Procedure SELECT( k,S) { if ISI =1 then return the single element in S    else { choose an element randomly from S;               let S1, S2, and S3 be the sequences of elements in S                 less than, equal to, and greater than m, respectively;              if IS1I >=k then return SELECT(k,S1)              ...
Let A be an integer array of length n. Design a divide and conquer algorithm (description...
Let A be an integer array of length n. Design a divide and conquer algorithm (description and pseudo code) to find the index of an element of the minimum value in the array. If there are several such elements, your algorithm must return the index of the rightmost element. For instance, if A = {0,2,4,5,2,0,3,10}, then the algorithm should return 5, which is the index of the second 0.
1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element...
1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element in an array of n numbers. 5. Find the order of growth for solutions of the following recurrences. a . T(n)=4T(n/2)+n,T(1)=1 b. T(n)=4T(n/2)+n2,T(1)=1 c. T(n)=4T(n/2)+n3,T(1)=1
Design and analyze a divide-and-conquer algorithm for finding the maximum element in a list: L[0: n – 1].
The following submission rules apply:·    For those questions requiring programs, the solutions must be implemented using JavaScript or Java.o Appropriate self-documenting comments in the source code are mandatory, consistent with good programming practices.o Solutions must be provided in plain text so that formatting is not lost.·    All answers must be provided in this document.·    Sources must be given accurate and complete citations sufficient for the instructor to find and confirm them.Design and analyze a divide-and-conquer algorithm for finding the maximum...
In class we saw that the fast divide and conquer integer multiplication method takesO(nlog3) time. Thisis...
In class we saw that the fast divide and conquer integer multiplication method takesO(nlog3) time. Thisis because the the shifts and additions can be done in time O(n). Suppose we use a very inefficient method for doingthe shifts and additions, so these take timeO(n2). With this slower method for doing the shifts and additions, figureout how much time will the divide and conquer integer multiplication method take . You can assume that nothing elsechanges in the divide and conquer integer...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find the index of the negative integer, and compute its average complexity.
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds...
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds the largest and second largest value from a vector of ints. void LargestTwo (vector l, int left, int right, int & largest, int & secondLargest) • Please write comment for your functions, similar to the one in the pseudocode, to include both pre- and post-conditions. • Comment on the logic of your code, e.g., what is true after the two recursive calls? • Answer...
need a code MIPS assembly language program to implement algorithms of an 8-bit integer "positive integer"...
need a code MIPS assembly language program to implement algorithms of an 8-bit integer "positive integer" to calculate the square root for an-8 bit integer using The Radix-2 SRT-Redundant and Non-Redundant Algorithm to approximate square root
Programming language: JAVA First, implement a recursive, Divide&Conquer-based algorithm to identify both the Minimum and Maximum...
Programming language: JAVA First, implement a recursive, Divide&Conquer-based algorithm to identify both the Minimum and Maximum element in an unsorted list. Second, convert your recursive algorithm to a non-recursive (or iterative) implementation. For your input, populate an "unsorted list" with random elements between 1 and 1,000,000.
Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n]...
Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n] and B[1 . . . n] and an integer k. Describe an algorithm to find the kth smallest element in the union of A and B in O(log n) time. For example, if k = 1, your algorithm should return the smallest element of A ∪ B; if k = n, your algorithm should return the median of A ∪ B.) You can assume...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT