The Count sort is a non-comparison based sorting algorithm, which was proposed by Harold H. Seward in 1954. Its advantage is that when sorting integers within a certain range, its complexity is 0 (n+k) (where K is the range of integers), faster than any comparison sorting algorithm.
The main idea of the algorithm analysis : According to the array elements of the value of the order, and then statistics is greater than the number of elements of an element, and finally can get an appropriate position of an element, such as: array[4] = 9; The number of elements less than array[4] is: 8; RRAY[4] = 9 should be placed in the 8th position of the element; the main step: 1, according to array, the corresponding element value corresponds to the position of Tmparray; 2, then according to the T The Mparray array elements are statistically larger than the number of elements of array arrays; 3, finally, according to the elements of the previous step, to find the appropriate location for the array element, temporarily deposited into the TMP array;
As shown in the following image: Array is the tmparray to be sorted; The TMP is a temporary array, and the array of arrays is saved;
Note: The count ordering is strict with the INPUT element because the array element value is used as the subscript for the Tmparray array, so if the array element value is 100, then the Tmparray array will apply 101 (including 0, which is Mix-min + 1).
Code Implementation
<pre name= "code" class= "CPP" > #include <stdio.h> #include <stdlib.h> void print_array (int *array, int l
ength) {int index = 0;
printf ("array:\n");
for (; index < length; index++) {printf ("%d,", * (Array+index));
} printf ("\ n");
} void Countsort (int *array, int length) {/* int *tmparray = (int*) malloc (sizeof (int) *length);
int I, j, Count;
for (i = 0; i < length; i++) tmparray[i] = 0; for (i = 0; i < length; i++) {for (count = 0, j = 0; J < length; J + +) {if (Array[i] < array [j])
count++;
} while (Tmparray[count]) count++;
Tmparray[count] = Array[i];
} for (i = 0; i < length; i++) array[i] = Tmparray[i];
Free (Tmparray); */int *tmparray = (int*) malloc (sizeof (int) * (length+1));//Request memory space, remember size is length + 1 (because the array element value is 0~9) int tmp[lengt
h];
int I, j, K; for (i = 0; i < lengtH
i++) {//initialize array tmparray[i] = 0;
Tmp[i] = 0; } for (i = 0; i < length; i++) tmparray[array[i]]++; Indicates the number of elements in the bucket for (i = 1; I <= length; i++) Tmparray[i] + + tmparray[i-1];//statistics is greater than the number of elements for the element for (i = le Ngth; i > 0;
i--) {//This is the core code, which can be understood as storing the elements in the array arrays in the appropriate position tmp[tmparray[array[i-1]]-1] = array[i-1]; tmparray[array[i-1]]--; Resolve a bucket with multiple elements} for (i = 0; i < length; i++) array[i] = tmp[i];//put the ordered elements back into the array (Tmparray
);//Whether space} int main (void) {//int array[] = {12, 1, 32, 201, 9987, 5, 10, 10090, 123, 453};
int array[] = {2, 1, 3, 0, 9, 5, 1, 7, 4};
int length = (sizeof (array))/(sizeof (array[1]));
Print_array (array, length);
Countsort (array, length);
Print_array (array, length);
return 0; }
Operation Result:
Complexity of Time
time complexity can be very good to see is: O (n);
Complexity of Space the spatial complexity can also be well seen: O (n);
Summary The time complexity and spatial complexity of count sequencing are very effective, but the algorithm has limited requirements for the input elements, so not all sorts use the algorithm; the best is that the difference between the 0~9 is not very large between the data elements; some people would say it's not much use, But in the subsequent cardinality sort, it can be seen as a basis in the cardinal sort;
Reprint please indicate the author and source, the original address: http://blog.csdn.net/yuzhihui_no1/article/details/44561487 If there is not correct, I hope you correct, learn together. Thank you...