Common Understanding: by establishing a connection between the array to be sorted and the sub-mark of the secondary array, the secondary array stores the number of each element of the original array equal to its own subscript, then, perform some operations to ensure the stability of counting sorting (you may not understand it well enough. Please submit your opinion ). Watch the dynamic process
[Cpp]
Int count_sort (int * const array, const int size)
{
Int * count;
Int * temp;
Int min, max;
Int range, I;
Min = max = array [0];
For (I = 0; I <size; I ++)
{
If (array [I] <min)
Min = array [I];
Else if (array [I]> max)
Max = array [I];
}
Range = max-min + 1;
Count = (int *) malloc (sizeof (int) * range );
If (NULL = count)
Return 0;
Temp = (int *) malloc (sizeof (int) * size );
If (NULL = temp)
{
Free (count );
Return 0;
}
For (I = 0; I <range; I ++)
Count [I] = 0;
For (I = 0; I <size; I ++)
Count [array [I]-min] ++; // records the number of values equal to the array subscript
For (I = 1; I <range; I ++)
Count [I] + = count [I-1]; // store the subscript value of your array at the position corresponding to the target array to ensure stability
For (I = size-1; I> = 0; I --)
Temp [-- count [array [I]-min] = array [I]; // store the original array in size order to another array
For (I = 0; I <size; I ++)
Array [I] = temp [I];
Free (count );
Free (temp );
Return 1;
}
The running time is bytes (n + k)