[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

[PC C++] Quicksort

Post by CompWiz »

Ok, so I ended up using a quicksort for a project that the boss wanted me to do a bubble sort on (and the writeup said to use a selection sort). Now, I have to show him that my quicksort is worth it, and is faster. I know I am not all that great of a coder, and my code is not all that optimized. So, if you wouldn't mind looking through my code and letting me know if I can change anything to make it better, let me know. Or, maybe a different sort that would run faster.

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. :)
In Memory of the Maxcoderz Trophy Image
coelurus
Calc Wizard
Posts: 585
Joined: Sun 19 Dec, 2004 9:02 pm
Location: Sweden
Contact:

Post by coelurus »

Does it work? Then I have nothing to add, because sorting a list of 100 elems is usually what you use Bubblesort for (since it takes a couple of seconds or so to write) or maybe selection/linear sort (since they take just a few seconds more).
You could try Radix sort, but I'd consider this problem over-solved :)
Andy_J
Calc Master
Posts: 1110
Joined: Mon 20 Dec, 2004 10:01 pm
Location: In the state of Roo Fearing
Contact:

Post by Andy_J »

Or a tree sort... ;)

I honestly can't remember how to implement a specific sort by memory. You can always just look it up =/ I might be able to do bubble sort, I haven't tried in a while (I should practice sorts more often). But yeah, bubble sort would work just fine for 100 ints...
ImageImage
Image
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

If you look up the particular search algorithm on Wikipedia, it'll usually have sample code (or pseudo-code) to copy. :|
CoBB
MCF Legend
Posts: 1601
Joined: Mon 20 Dec, 2004 8:45 am
Location: Budapest, Absurdistan
Contact:

Post by CoBB »

You should sort the array in place instead of creating two new arrays in every recursion, because the current one can become quite memory intensive as the array gets bigger (it can use O(N^2) memory in the worst case!). The very reason quicksort is so popular is that it allows this trick.

You can maintain two indices, one starting at the first element and one at the last. The former should be moved right until you find an element that's greater than the pivot, the latter should be moved left until it hits an element less than or equal to the pivot. Swap these two elements and continue moving and swapping in the same manner until the two indices hit each other. At that point the first half of the array contains elements not greater than the pivot, and you can pass the starting and ending indices recursively (along with the whole array, of course). And forget null terminated arrays, it's an ugly hack that begs for bugs. You can't use them anyway with this method.

But since you are using C++, why don't you simply use STL containers and algorithms? If you are told not to, what's the reason?
CompWiz
Calc King
Posts: 1950
Joined: Thu 13 Oct, 2005 1:54 pm
Location: UB

Post by CompWiz »

I guess I'll try doing the in place sorting. Like I said, I'm not all that good at this. Thanks for the help. :)

And yes, I do have to write it myself, and also, now that I used a quicksort, I have to show my boss the speed of it vs a bubble sort. Basically, if you do something different than he does it, you have to compare the two.
In Memory of the Maxcoderz Trophy Image
Andy_J
Calc Master
Posts: 1110
Joined: Mon 20 Dec, 2004 10:01 pm
Location: In the state of Roo Fearing
Contact:

Post by Andy_J »

If your boss doesn't understand the difference between a quicksort and bubblesort (both of which are core to computer science), I would start looking for a new job.
ImageImage
Image
CompWiz
Calc King
Posts: 1950
Joined: Thu 13 Oct, 2005 1:54 pm
Location: UB

Post by CompWiz »

Yeah, but my job is really cushy. I come in when I feel like it, and just count how many hours I'm in for(to figure out how much pay I get). There's no taxes(because of the way I'm being paid) and all the university students on the floor run big multi-player games like battlefield 2, counter strike, or Age of Mythology every day.

Good luck finding another job like that. I could care less if my boss knows the differences between a quicksort and a bubblesort.
In Memory of the Maxcoderz Trophy Image
chickendude
Extreme Poster
Posts: 340
Joined: Fri 07 Jul, 2006 2:39 pm

Post by chickendude »

You couldn't care less? ;) What exactly do you do, then, besides play video games and sort integers?
CompWiz
Calc King
Posts: 1950
Joined: Thu 13 Oct, 2005 1:54 pm
Location: UB

Post by CompWiz »

well, that's about it, and it's mostly video games, with a little sorting on the side, all while getting paid. :mrgreen:
In Memory of the Maxcoderz Trophy Image
coelurus
Calc Wizard
Posts: 585
Joined: Sun 19 Dec, 2004 9:02 pm
Location: Sweden
Contact:

Post by coelurus »

I'd love a job like that beside studying...

Why do you have to make your own sorter? Is it a "real" project where this sorter will actually be of use?
CompWiz
Calc King
Posts: 1950
Joined: Thu 13 Oct, 2005 1:54 pm
Location: UB

Post by CompWiz »

possibly. It's more of programming a bunch of stuff I'll need to know how to do when I start doing the real work.
In Memory of the Maxcoderz Trophy Image
User avatar
Dwedit
Maxcoderz Staff
Posts: 579
Joined: Wed 15 Dec, 2004 6:06 am
Location: Chicago!
Contact:

Post by Dwedit »

I'm more of a merge-sort fan myself, since it has zero stack usage if coded right, and always runs in n*lg(n) time no matter what. The extra memory usage can be reduced heavily by just using an index table instead of copying the full data.

Yeah, you can tell I've been working on serverely stack-constrained systems lately. Mergesort is nice because you know exactly how much stack it will use: 0.
You know your hexadecimal output routine is broken when it displays the character 'G'.
CompWiz
Calc King
Posts: 1950
Joined: Thu 13 Oct, 2005 1:54 pm
Location: UB

Post by CompWiz »

how easy is mergesort to code?

as for the quicksort, I'm about halfway through coding the in-place version.
In Memory of the Maxcoderz Trophy Image
coelurus
Calc Wizard
Posts: 585
Joined: Sun 19 Dec, 2004 9:02 pm
Location: Sweden
Contact:

Post by coelurus »

Mergesort is one of the few sorts I have never written by myself. It's "simple", split an array into two all the way down until the pieces are small enough to be sorted by hand. Walk back up, merge the pieces two by two for every level in the recursion and sort them. As DWedit implied, which applies to pretty much all binary structures, it's possible to calculate offsets and lengths for the split pieces directly without doing any recursive calls.

To speed up the development of the quicksort, search for "quick sort" on google, hit "I'm feeling lucky" and look at the source code there :)
Post Reply