Base 2 is actually sorted by the base because it is very fast. Is the O (n) level, so even if it is called the foundation 2 fast row :)
Its basic principles are bucket rows, but everyone wants to know how to eat memory in the barrel .... I want to row 32-bit integers to need 4GB buffer ... Horror, so there is time to change space ~ reduce space overhead Multi-painted a little time. The base sort is actually more than the barrel.
What is the base sort? Based on everyone should know .... For example, the 10-based base is 10. How is the number of 10 yuan? It is definitely the highest bit first, then develops to the development ... The base sorting and this principle are the same. However, we prefer to choose the integer power of 2 as a base ~ Because the integer power of 2 can be displaced ~
OK. Not much nonsense. It is said that the idea of the base 2 fast drain function (the following is pseudo code).
We assume that the function entry passes into the original number group SRC and the length N. So, I must open a counter (because after all, it is based on the barrel-row ~) count, there is a temporary array TEMP [N]. We use 2 ^ 8 as the base, sorted 32-bit integers as an example.
For 4 times (i = 0, 1, 2, 3) {ZeromeMory (count); // Obviously, the Memcpy (TEMP, SRC, N * 4) of the counter is to be emptied; // The following operation is the TEMP operation Finally, TEMP is originally copied back to SRC to FOR K = 0 to N-1 { count [(Temp [k] >> (8 * i)) & 0xff]; // Here to tub}
Entry_point = 0; // This variable records the position in the array after each counter data is arranged
For k = 0 to 256 {_temp = count [k]; count [k] = entry_point; // turn the corresponding data number into the position of the corresponding data in an array ENTRY_POINT = _ TEMP; // Obviously, to put base entry_point Push-back ~}
Fork = 0 to nsrc [count [(Temp [k] >> (8 * i)) & 0xff] ] = TEMP [K]; // This is a bit difficult to understand. This statement means that this number is copied back to the original array according to the number of "bit" (equivalent to 10-based fame, ten) according to the array, so that this "bit" is sequenced.
}
Finally, send an example code ... may be wrong ... and everyone can see that even the language level is indeed 8 lines, welcome everyone to make a brick