Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

QuickSort/InsertionSort 1

Status
Not open for further replies.

keat

Programmer
Jan 16, 2002
3
0
0
US
I need to write a program to test the code below using an aray of 200,000 integers. I also need to use the clock() function before and after the call to QuickSort and subtract to calculate the CPU time spent sorting. This program needs to run this time trial 10 times. Here is the QuickSort/InsertionSort program. Thanks so much for your help.

Keat.

void Quicksort(dataType A[], int F, int L)
{
int PivotIndex;
if (F<L)
{
//is this a small region???
if ((L - F) < Cutoff) {
//yes.....so use InsertionSort
InsertionSort(A, F, L)
}
else
{
//no...do normal QuickSort
Partition(A, F, L, PivotIndex)
Quicksort(A, F, PivotIndex-1)
Quicksort(A, PivotIndex+1, L);
}
}
}


 
Try this, but the question is kinda vague. hope this helps.

#include <time.h>
#include <stdlib.h>
#include <iostream.h>

void Quicksort(dataType A[], int F, int L)
{
int PivotIndex;
if (F<L)
{
//is this a small region???
if ((L - F) < Cutoff) {
//yes.....so use InsertionSort
InsertionSort(A, F, L)
}
else
{
//no...do normal QuickSort
Partition(A, F, L, PivotIndex)
Quicksort(A, F, PivotIndex-1)
Quicksort(A, PivotIndex+1, L);
}
}
}
void main () {
const size=200000

int *testarray=new int [size];
for (int i=0;i<size;i++){
testarray=rand();//fill up the test array with random numbers
}

clock_t start , finish;

start=time();//start the times

Quicksort(testarray,0,size-1);//sort the array

finish = time();//end the timer

cout<<&quot;Elapsed CPU time =&quot;<<finish-start<<endl;

}//end of main

Pahjo Pahjo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top