As for the data it will be sorting, it is an array of 100 integers generated by the rand function(random numbers), but it may be tested for speed with more. Here is the code:
Code: Select all
void quickSort(int unsorted[100]){
int left[100];
int right[101];
Lcount = 0;
Rcount = 0;
if(unsorted[1]!=0){
pivot = unsorted[0];
for(counter = 1; unsorted[counter]!=0; counter++){
if(unsorted[counter]<pivot)
left[Lcount++]=unsorted[counter];
else
right[Rcount++]=unsorted[counter];
}
left[Lcount++]=pivot;
left[Lcount]=0;
right[Rcount]=0;
quickSort(left);
quickSort(right);
}
else if(unsorted[0]!=0){
sorted[sortedCounter++]=unsorted[0];
if(sortedCounter>99){
output();}
return;
}
else
return;
}
In case you can't follow it, here is how it works:
if the second element of the list is not zero, then:
use first element as pivot. If next element is less than or equal to pivot, put it in the left list. If greater, put it in the right list. For loop through all elements, checking for 0 marking the end of the numbers.
put the pivot in the right list, to eliminate problems with numbers that are the same.
put 0 marker at the end of both the right and left lists. run the function with the left and right lists, in that order.
else, if the first element is not = to zero, put it into the sorted list, incrementing the sorted counter.
if the sorted counter is >99, run the write out program.
return
else(if the first element is 0)
return
I really appreciate any advice you have. Thanks.