[PC C++] Quicksort

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

CompWiz
Calc King
Posts: 1950
Joined: Thu 13 Oct, 2005 1:54 pm
Location: UB

Post by CompWiz »

Ok, I got the in-place quicksort running. Let me know what you think of the code. :)

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;
}
also, the boss wanted me to compare it to a bubble sort, so I wrote up this nice, elegant bubble sort.

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;
}
Notice, they're even commented. :shock: (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. :D

Thanks to everyone for the advice and help. :)
In Memory of the Maxcoderz Trophy Image
CoBB
MCF Legend
Posts: 1601
Joined: Mon 20 Dec, 2004 8:45 am
Location: Budapest, Absurdistan
Contact:

Post by CoBB »

Nice job!

You should have run bubble sort for ten million elements too, the difference would have been even more spectacular. ;)

By the way, there's a quicksort implementation in my thesis too (does basically the same, but looks more compact than yours), both in C and in Haskell. :P
CompWiz
Calc King
Posts: 1950
Joined: Thu 13 Oct, 2005 1:54 pm
Location: UB

Post by CompWiz »

CoBB wrote:You should have run bubble sort for ten million elements too, the difference would have been even more spectacular. ;)
yeah, I was kind of hurrying to get this done before my meeting with my boss, so I didn't have years to sit around waiting for it. :)

I got my code working about 20 minutes before my meeting was set to take place, and in that time, I commented the code, fixed my flowcharts, tested it for speed, and made the exel spreadsheet and charts.
In Memory of the Maxcoderz Trophy Image
Post Reply