0.033 seconds of art ---- Radix sort
For personal use only, do not reprint, do not use for any commercial purposes.
I have discussed the issue of fast sorting in a previous article, and recently I have studied various sorting methods.AlgorithmAnd accidentally saw Radix sort. I tested it and guessed what the result was? Say "fast like hell" in the words of the last guy in gamedev "!
Radix sort can be said to be the most neglected sorting algorithm. When speaking about sorting in schools, we often only mention several well-known comparative sorting methods, even the introduction to algorithms is a little bit mentioned, and no specific implementation steps are provided. Here we have specific Radix sort algorithms. The principle should be simpler than quick sort. Here we have C ++ implementation.CodeIs also very concise, the following is the C # version:
Private Int64 [] array;
Private Int64 [] temparray;
Private Int [] Count = New Int [ 256 ];
Private Int [] Offsettable = New Int [ 256 ];
Public Void Radixsort ()
{
Radix ( 0 , Array, temparray );
Radix ( 1 , Temparray, array );
Radix ( 2 , Array, temparray );
Radix ( 3 , Temparray, array );
Radix ( 4 , Array, temparray );
Radix ( 5 , Temparray, array );
Radix ( 6 , Array, temparray );
Radix ( 7 , Temparray, array );
}
Private VoidRadix (IntByteindex, int64 [] source, int64 [] DEST)
{
Array. Clear (count,0,256);
IntByteoffset=Byteindex* 8;
For ( Int I = 0 ; I < Elementcount; I ++ )
{
Byte Radixvalue = ( Byte ) (Source [I] > Byteoffset );
Count [radixvalue] ++ ;
}
Offsettable [ 0 ] = 0 ;
For ( Int I = 1 ; I < 256 ; I ++ )
{
Offsettable [I] = Offsettable [I - 1 ] + Count [I - 1 ];
}
For ( Int I = 0 ; I < Elementcount; I ++ )
{
Int Index = Offsettable [( Byte ) (Source [I] > Byteoffset] ++ ;
Dest [Index] = Source [I];
}
}
Of course, Radix sort does not have any disadvantages. First, the value to be sorted should be an integer. Second, additional memory needs to be occupied, and most comparative sorting algorithms can be sorted in situ. Finally, it is recommended that the data volume be smaller than 10 KB (this value may change for different hardware ).
The following is the result of sorting int64 with q6600 (2.4g, 4 Core:
Quick Sort Radix sort
1 K1 ms 0 ms
2 K 1 ms 0 ms
5 K 2 MS 1 ms
10 K 4 ms 2 ms
50 K 18 Ms 13 Ms
100 k 38 Ms 35 ms
500 k 212 Ms 654 Ms
We can see that Radix sort has obvious advantages over data of less than 10 KB, especially when less than 2 kb, it is almost impossible to correctly measure the time. Note that int64 is used here. For Radix sort, 8 pass is required to complete sorting. For int32, 4 pass is required, and the theoretical speed is doubled. Unfortunately, when the data volume was too large, the performance of Radix sort experienced dramatic changes and no specific analysis was made. However, this is probably because of the CPU cache size. I did a test on another very old P4 2.4 (L2 512 K) machine. When the data exceeds 10 K, the advantage of Radix sort is not obvious.
Conclusion: For most games, the number of rendering queues is no more than 10 K, usually 2 ~ About 3 K. Therefore, it is worthwhile to try Radix sort for sorting.