Objective
The last three sorting algorithms, because they are not based on the comparison of the sort, so that these three sort algorithms can be run in a linear time. However, because of the particularity of restrictive conditions, the application surface does not have a wide ranking algorithm based on element comparison, but it is quite useful and highly efficient in many specific cases.
Count sort
The counting order is based on the premise that each of the N input elements is an integer in the range 0 through K, where K is an integer. So the program we write later is just sorting between the elements from 0 to K, in other words, there must be no negative numbers in the sort element.
The basic idea of counting sort is: for an INPUT element x, first determine the number of elements that are small to x in all the input elements, then the position of the x in the order is clear. For example, if there are 10 elements in all input elements less than x, then the position of x in the sequence should be 11. Of course, if you have the same elements, you naturally want to place them in the adjacent position.
The introduction of the algorithm gives a very detailed pseudo code of the counting sort, and we set the array arr as an input array based on this pseudocode, arr each element value between 0 and K, brr the sorted output array, CRR records the number of occurrences of each element in the arr. Write the code as follows:
/* The
first form to implement count sort
count sorted order for small to large
arr[0...len-1] for rows array, each element is a value in 0-k
brr[0...len-1] for sorted output array
CRR[0...K] Saves the number of times each value in the 0...K appears in the array arr
/void count_sort (int *arr,int *brr,int *crr,int len,int k)
{
int i,j=0;
Array CRR elements are placed 0 for
(i=0;i<=k;i++)
crr[i] = 0;
Counts the number of recurring occurrences of each element in the array arr for
(i=0;i<len;i++)
crr[arr[i]]++;
The number of elements less than or equal to I in the array arr for
(i=1;i<=k;i++)
crr[i] + = crr[i-1];
Place the elements in the ARR in the corresponding position in the BRR for
(i=len-1;i>=0;i--)
{
brr[crr[arr[i]]-1] = arr[i];
If you have the same element, put it in the next location
crr[arr[i]]--
}
Obviously the time complexity of the above code is O (n+k), but using the BRR to save the sorting result, we can make some improvements to make the sort in place, as follows:
/* The
second form to implement count sort
count sorted order for small to large
arr[0...len-1] for rows array, each element is a value in 0-k
CRR[0...K] Save 0 ... The number of occurrences of each value in the array arr in K
/void count_sort (int *arr,int *crr,int len,int k)
{
int i,j=0;
Array CRR elements are placed 0 for
(i=0;i<=k;i++)
crr[i] = 0;
Counts the number of recurring occurrences of each element in the array arr for
(i=0;i<len;i++)
crr[arr[i]]++;
Place element I into arr appropriate location for
(i=0;i<=k;i++) while
((crr[i]--) >0)
{
arr[j++] = i
}
} According to the size of crr[i]
Test with the following code:
int main ()
{
int i;
Array to be sorted, each element is between 0-8
int arr[] = {2,1,3,8,6,0};
int brr[6];
int crr[9];
Count_sort (arr,brr,crr,6,8);
printf ("The result of the count sort is:");
for (i=0;i<6;i++)
printf ("%d", Brr[i]);
printf ("\ n");
return 0;
}
The test results are as follows: