![]() |
| [[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]] |
|
|
#1 |
|
Posts: n/a
|
I was trying to sort data in a different way and thought of this algo.
It's obviously better than bubble sort in term of efficiency. But I still need to do more R&D to compare this algo with the others available in the market. Would appreciate your comment. Code:
|
|
|
|
#2 |
|
Valued Member
Join Date: Aug 2005
Posts: 189
|
seems still O(n^2) to me ...
nice name for the sort though ![]()
___________________________________________
hi, i'm a signature viruz, plz set me as your signature and help me spread :) Bramz' warehouse | LiAR isn't a raytracer |
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Oct 2005
Location: Pensacola, FL
Posts: 1,028
|
What's wrong with std::sort(&iArray[0], &iArray[ARRAY_SIZE - 1]);? Or qsort from stdlib.h if you are using C.
Google for "quick sort" or "heap sort" if you want to implement a better algorithm on your own.
___________________________________________
monjardin's JwN Meter (1,2,3,4,5,6): |----|----|----|----|----|----|----|----|----|----| * Last edited by monjardin : 08-17-2006 at 10:20 AM. |
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Oct 2005
Location: Waterville, MN
Posts: 424
|
Natural merge sort is another good one if you have enough memory to double-buffer the array.
|
|
|
|
|
|
#5 |
|
Valued Member
Join Date: Mar 2005
Posts: 135
|
Or just aim for the throat and implement a distribution sort to break the n.log(n) barrier.
|
|
|
|
|
|
#6 |
|
Senior Member
Join Date: Sep 2005
Location: Hamburg / Germany
Posts: 597
|
Radix sort has O(n), and it's dead easy to implement :-)
Sucks for strings though. |
|
|
|
|
|
#7 | |
|
Valued Member
Join Date: Oct 2005
Location: Switzerland
Posts: 117
|
Quote:
Or even proove P=NP. Should be easy till tomorrow ![]() |
|
|
|
|
|
|
#8 | |
|
Valued Member
Join Date: Aug 2004
Location: Norway
Posts: 200
|
Quote:
Nah, it's constant O(n log n). I would probably preffer quicksort since with a few quircks it seems to be almost always linear (and my quicksort is faster than std::sort() ). |
|
|
|
|
|
|
#9 | |
|
Valued Member
Join Date: Mar 2005
Posts: 135
|
Quote:
Code:
Thanks for playing. |
|
|
|
|
|
|
#10 | |
|
Valued Member
Join Date: Aug 2005
Posts: 189
|
Quote:
Care to explain how you come to that? Seems to me he's traversing only two elements less per recursion step ... n + n-2 + n-4 + n-6 + ... = n*(n+1)/4 (for even n) oh, and constant doesn't really match with O(n log n). Constant = O(1) ![]()
___________________________________________
hi, i'm a signature viruz, plz set me as your signature and help me spread :) Bramz' warehouse | LiAR isn't a raytracer Last edited by bramz : 08-18-2006 at 05:59 AM. |
|
|
|
|
|
|
#11 | |
|
Valued Member
Join Date: Aug 2004
Location: Norway
Posts: 200
|
Quote:
Well, O(n^2) was what I meant (I'm slightly stupid today).By constant I reallly meant that the best-case has the same complexity as worst case. |
|
|
|
|
|
|
#12 |
|
Valued Member
Join Date: Nov 2004
Location: Milan -ITALY-
Posts: 297
|
Ditto.
It also sucks when used on small arrays in general. Otherwise it's lightning fast.
___________________________________________
-Nautilus 1.551640271931635485e+1292913986 ? Why, that's: 2 ^ ((2 ^ (2 ^ ((2 ^ 2) + (2 ^ (2 - 2))))) - (2 ^ (2 - 2))). Now verify, please... |
|
|
|
|
|
#13 | |
|
Valued Member
Join Date: Oct 2005
Location: Switzerland
Posts: 117
|
Quote:
|
|
|
|
|
|
|
#14 | |
|
Valued Member
Join Date: Mar 2005
Posts: 135
|
Quote:
Last edited by tbp : 08-20-2006 at 09:53 AM. |
|
|
|
|
|
|
#15 | |
|
Valued Member
Join Date: Oct 2005
Location: Switzerland
Posts: 117
|
Quote:
|
|
|
|
|
|
|
#16 |
|
Valued Member
Join Date: Mar 2005
Posts: 135
|
If you will, but it's not that arbitrary if you have to equip it with a predicate. So the distinction isn't that clear.
|
|
|
|
|
|
#17 |
|
Member
Join Date: Mar 2006
Posts: 38
|
Try looking for RADIX SORT... According to an old text book I dug up this is
the only known O(n) algorithm... if the radix used is 2 then you can sort an array of 'k' unsigned integers, maximum value 'm' in ceil(log2(m)) * n data moves, ie. O(n)... here's a quick (and nasty) implementation I knocked up... it needs a buffer as a temporary store at least as big as the unsorted array, but the code is so simple as to not need much explanation. Also it is ideally suited to a fast assembler implementation.... struct CSortEntry { unsigned int key; // append other data here... }; void CGeneric::Sort( CSortEntry * pArray, int iCount, unsigned int iMaxKey) { unsigned int iMaxMask = 1; while( iMaxMask <= iMaxKey ) iMaxMask += iMaxMask; unsigned int iMask = 1; while( iMask < iMaxMask ) { unsigned int Count1 = 0; CSortEntry * inPtr = pArray; CSortEntry * outPtr0 = inPtr; CSortEntry * outPtr1 = CSortArrayTemp; for( int i = 0; i < iCount; i++ ) { if( (inPtr->key & iMask)==0) { *outPtr0++ = *inPtr++; } else { *outPtr1++ = *inPtr++; Count1++; } }; // Weld the two parts together memcpy( outPtr0, CSortArrayTemp, (Count1)*sizeof(CSortEntry) ); // Advance mask, to next bit iMask += iMask; }; }; |
|
|
|
|
|
#18 |
|
Valued Member
Join Date: Mar 2005
Posts: 135
|
Distribution sorts are what we've been discussing for a couple of posts already. Doing 1 bit per pass is truely horrible.
Empirically i've found that on current x86, radix sorting 32bits in 3 pass is generally optimal. You can save some memory by doing only 2 passes, but then you can't keep histograms in cache and they have more chances to be larger than the data you're sorting ![]() |
|
|
|
|
|
#19 |
|
New Member
Join Date: Dec 2007
Location: Petaluma, California
Posts: 1
|
I personally like doing radix sorts in 2 or 4 passes for two reasons:
1) If you know the endianness of the platform, you can just cast the memory pointer of the key value to a byte pointer and use the pass index (or 3-pass) to get the right value to sort by. (shifting & masking doesn't cost much either and is platform safe, but I like to be difficult) 2) Doing an even number of passes means that your last pass can transfer the sorted data back into your source array without requiring an extra copy. This assumes you want to sort 'in place', which I usually do. As for the onion sort, it really is just a variant of a bubble sort. And, since the code is tail recursive you could implement it in a while loop instead of using all the extra stack space. |
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|