Posted: Thu 10 Aug, 2006 3:04 pm
Ok, I got the in-place quicksort running. Let me know what you think of the code.
also, the boss wanted me to compare it to a bubble sort, so I wrote up this nice, elegant bubble sort.
Notice, they're even commented. (my boss said I had to. :frustrated: )
I compared the speed, and here are the results I showed my boss, with nice excel graphs and a spreadsheet:
Quicksort sorting time:
10000 numbers- 0.01 seconds
20000 numbers- 0.04 seconds
100000 numbers- 0.081 seconds
200000 numbers- 0.161 seconds
1000000 numbers- 1.241 seconds
10000000 numbers- 24 seconds
Bubblesort sorting time:
10000 numbers- 1.212 seconds
20000 numbers- 33.96 seconds
100000 numbers- 174.266 seconds
200000 numbers- 919.665 seconds
I'd say that showed my point nicely.
Thanks to everyone for the advice and help.
Code: Select all
void quickSort(int start, int end){
int left=start +1;//sets up left to start at the element after the pivot
int right = end;//sets up right to start at the last element in the part of the array being sorted
pivot = original[start];//sets the pivot as the first element in the part of the array being sorted
do{
for(left; left<=end && original[left]<=pivot; left++){}//runs left to right across the array until an element with a value higher than the pivot is found, or left exceeds end
for(right; right>start && original[right]>=pivot; right--){}//runs right to left across the array until an element with a value lower than the pivot is found, or right decreaces past start
if(left<right){//if the higher value is left of the lower value, switch them
temp=original[left];
original[left]=original[right];
original[right]=temp;
}
} while(left<right);//do while the left is lower than the right. If it is higher, then the array has been sorted around the pivot
original[start]=original[right];//swap the pivot with the last element of the elements lower than the pivot
original[right]=pivot;
if(right-1>start)//if there is something to sort on the low side
quickSort(start, right-1);//sort it
if(++right<end)//if there is something to sort on the high side
quickSort(right, end);//sort it
return;
}
Code: Select all
void bubbleSort(int end){
int swap;
do{
end --;//decrement end, so it indicates the last element in the array that hasn't been sorted
swap = -1;//sets swap to -1 as a flag
for(int index =0; index <end; index++){//run through the unsorted elements of the list
if(original[index]>original[index+1]){//if the two consecutive elements are in the wrong order, swap them
swap=original[index];
original[index]=original[index+1];
original[index+1]=swap;
}
}
} while(swap != -1);//if swaps have been made, keep sorting
return;
}
I compared the speed, and here are the results I showed my boss, with nice excel graphs and a spreadsheet:
Quicksort sorting time:
10000 numbers- 0.01 seconds
20000 numbers- 0.04 seconds
100000 numbers- 0.081 seconds
200000 numbers- 0.161 seconds
1000000 numbers- 1.241 seconds
10000000 numbers- 24 seconds
Bubblesort sorting time:
10000 numbers- 1.212 seconds
20000 numbers- 33.96 seconds
100000 numbers- 174.266 seconds
200000 numbers- 919.665 seconds
I'd say that showed my point nicely.
Thanks to everyone for the advice and help.