Sorting algo's

Started by
20 comments, last by duhroach 21 years, 6 months ago
Hey all, what''s some of the quickest sorting algo''s for acendingly sorting an array of ints. ~Main == Colt "MainRoach" McAnlis Programmer www.badheat.com/sinewave
==Colt "MainRoach" McAnlisGraphics Engineer - http://mainroach.blogspot.com
Advertisement
The two quickest I know are merge sort and quick sort. I believe quick sort is typically the faster of the two. But quick sort can get slower if the list is already somewhat sorted. And quick sort isn''t always the best tool for the job. I''ve had cases where insertion sort was a better choice. Never have had any reason to use bubble sort, though.
quicksort (what a surprise ! )

in C, use qsort()
in C++, use std::sort()

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I''m no expert on sorting, but perhaps some kind of bucket sort would apply to your situation? Depends of course on how much you really care for speed.

Miles
Depends;

If the ints are all in a fixed range, e.g. between 0 and 100, they can be sorted very efficiently (particularly for a large number of them), I think it''s called bucket sort.

Otherwise, quicksort or merge sort should be fine.

Also, if the ints are already mostly sorted then you''ll want to pick an appropriate algorithm, I can''t quite think what right now but I''m sure someone can tell you.
quote:Never have had any reason to use bubble sort, though.

Sofisticated implementations of the quick-sort algorithm don''t sort any blocks smaller than a threshold size, and bubble sort is used to finish-up the job at the end.

Actually opened up a book and looked over the syntax for all the algos, quicksort is probally one of the faster for what i'm trying to do.
Rather than making my own algo, i went ahead and used the STD function.
NOTE: Does that create more overhead?


  struct FrontToBackSort{	bool operator()(object*& rpStart, object*& rpEnd)     {          return rpStart->z < rpEnd->z;     };};void ZSortObjectList(){	std::sort(o_list.begin(),o_list.end(),FrontToBackSort());}   

That should work.. i think.

~Main

==
Colt "MainRoach" McAnlis
Programmer
www.badheat.com/sinewave

[edited by - duhroach on October 4, 2002 1:17:50 PM]
==Colt "MainRoach" McAnlisGraphics Engineer - http://mainroach.blogspot.com
merge/heap sort have complexity O(n*logn) worst case; quick sort is O(n*logn) expected case, and O(n*n) worst case.
counting sort (what ya''ll call bucket sort, although I thought that''s something else) and radix sort are O(n).

If you care about speed, rolling your own will always end up being faster. (execution time, not programming time )
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3
If your integers aren''t too big, and they aren''t repeated, you can make an array

int sortedints[MAX_VALUE];

and then use the value of the integers as indices into this array.

Cédric
quote:Original post by duhroach
Rather than making my own algo, i went ahead and used the STD function.
NOTE: Does that create more overhead?


No, the comparison function will be inlined in the sort loop. You should be ok. At any rate, you can always benchmark the code.


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement