Question

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.

Homework Answers

Answer #1

#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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
write program to compute intersection of two sorted array of integers and compute the CPU time...
write program to compute intersection of two sorted array of integers and compute the CPU time for different sets of unsigned integers generated by a random number generator test this using the same data sets: atleast 3 of size 1000 integers, atleast 3 of size 10000 integers, atleast 3 of size 100000 integers, atleast 3 of one million integers and atleast 3 of size 10 million integers DONT FORGET CPU TIME FOR EACH ONE Java
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b,...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b, where a – b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a – b = 3 A = [10, 4, 6, 16, 1, 6, 12, 13] Then your method should return the following pairs: 4, 1 15, 12 13, 10 A poor solution: There are...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...
1.A fair die is rolled once, and the number score is noted. Let the random variable...
1.A fair die is rolled once, and the number score is noted. Let the random variable X be twice this score. Define the variable Y to be zero if an odd number appears and X otherwise. By finding the probability mass function in each case, find the expectation of the following random variables: Please answer to 3 decimal places. Part a)X Part b)Y Part c)X+Y Part d)XY ——- 2.To examine the effectiveness of its four annual advertising promotions, a mail...
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...
IntList Lab Specifications You are required to come up with a single header file (IntList.h) that...
IntList Lab Specifications You are required to come up with a single header file (IntList.h) that declares and implements the IntNode class (just copy it exactly as it is below) as well as declares the IntList Class interface only. You are also required to come up with a separate implementation file (IntList.cpp) that implements the member functions of the IntList class. While developing your IntList class you must write your own test harness (within a file named main.cpp). Never implement...
Please answer this question in short essay form (2-4 paragraphs) Considering that cultures as complicated and...
Please answer this question in short essay form (2-4 paragraphs) Considering that cultures as complicated and socially constructed through the communicative interaction of organizational members. Briefly describe how the organizational concepts of complicated, emergent, unitary, and ambiguous apply to the sample auto-ethnography. Sample Auto-ethnography: Required Reading Auto-ethnography of College X Joe Student Organizational Culture and Diversity 223-58000 “The organization’s culture has both a direct and an indirect impact on the allocation of power among diverse groups. The values and ideologies...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From the April 2004 Issue Save Share 8.95 In 1991, Progressive Insurance, an automobile insurer based in Mayfield Village, Ohio, had approximately $1.3 billion in sales. By 2002, that figure had grown to $9.5 billion. What fashionable strategies did Progressive employ to achieve sevenfold growth in just over a decade? Was it positioned in a high-growth industry? Hardly. Auto insurance is a mature, 100-year-old industry...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT