Implementation of counting sort and cardinal sort

Source: Internet
Author: User

Count sort

The principle of counting sorting

sets the sorted array to a, sorted and stored to b,c as a temporary array. The so-called count, first of all through an array C[i] calculates the number of elements equal to I, this process only need to iterate once, and on this basis, calculate the number of elements less than or equal to I, is also a heavy loop to complete. The next step is the key: reverse cycle, from length[a] to 1, place a[i] in the B c[a[i]] position. The principle is: c[a[i]] means the number of elements less than or equal to a[i], exactly where the a[i] should be sorted. And from Length[a] to 1 reverse cycle, you can ensure that the same elements of the relative order of the same, which is the count of sorting stability. Stability is very important when array a has attachment properties.

The premise and application scope of counting sort

    The element in a cannot be greater than k, and the element should be the subscript of an array, so the element should be a nonnegative integer. And if there is a large element in a, it is not possible to allocate large enough space. So the counting sort has the great limitation, its mainly applies to the number of elements, but generally not too large and always less than the case of K, in this case, the use of counting sorting can be achieved very high efficiency. Because the length of the array C used to count depends on the range of data in the array to be sorted (equal to the difference between the maximum and minimum values of the array to be sorted plus 1), this makes the count sort for arrays with a large data range, which requires a lot of time and memory. For example, a count sort is the best algorithm for sorting numbers between 0 and 100, but it is not appropriate to sort names alphabetically. However, the count sort can be used in the cardinality sort algorithm to sort a large array of data ranges.

When the INPUT element is an integer of n 0 to K, its run time is Θ (n + k). The count sort is not a comparison sort, and the sort is faster than any comparison sort algorithm.

Steps to count the sorting algorithm:

1. Find the largest and smallest elements in the array to be sorted

2. Count the number of occurrences of the element in the array for each value I, and deposit in the item I of array C

3. Accumulate all counts (starting with the first element in C, adding each item and the previous item)

4. Reverse-Populate the target array: Place each element I in the C (i) of the new array, subtract C (i) minus 1 for each element

Implementation code:

Void countsort (int *a, int size) {int min = a[0], max = a[0]; int i = 0;for  ( i = 0; i < size; i++) {if  (min  > a[i] {min = a[i];//Find the smallest number in the array}if  (Max < a[i]) {Max = a[i] ;//Find the maximum number in the array}}int range = max - min + 1;int *count = new  int[range];//Initialize array//memset (count, 0, sizeof (int) *range);for  (i = 0; i  < range; i++) {count[i] = 0;} Change the number of the array A to 0,1,2....for  (i = 0; i < size; i++) in the array count {// The corresponding position of the array count is made into numbers, representing the position there are several identical numbers//GCA made of 1, representing this position has a number//GCA made 2, representing this position has two identical number count[a[i] - min]++;} int j = 0;//restores the number in count back to array a, and it is sorted for  (i = 0; i < range;  i++) {//repeat n times, take Back n times while  (count[i]>0) {a[j++] = i + min;count[i]--;}} DeLete[] count;} 

Base sort

Algorithm idea:

Array to sort [62,14,59,88,16] simple point five digits

Allocate 10 barrels, the bucket number is 0-9, in the single digit number for the bucket number into the bucket, turn into the bottom so

|  0 | 0 |  62 | 0 |  14 | 0 |  16 |  0 | 88 | 59 |

|  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 | 9 | Bucket number

Take out the numbers in the bucket,

Output result: [62,14,16,88,59]

Again into the bucket, but this time with a 10-digit number, enter the corresponding bucket, into the following:

Because the first single-digit ordering, so when the 10-digit number is equal, the single digit is from small to large order into the bucket, that is, into the barrel or orderly

| 0 |  14,16 |  0 |  0 | 0 | 59 | 62 | 0 |  88 | 0 |

|  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 | 9 | Bucket number


Because there are no more than 100 of the number, there are no hundred numbers, so to this sort is complete, order out can be

Final output: [14,16,59,62,88]

Implementation code:

Gets the maximum number of digits Static int getmaxradix (int *a, int size) {Int radix = 10;int  count = 1;for  (int i = 0; i < size; i++) {//Note this must be " >= ", if your maximum number is 100, if//there is no" = ", you get the maximum bit is two bit while  (A[i]>=radix) {radix *= 10;count++;}} Return count;} Static void _radixsort (int* a, int size, int divisor, int* tmp) { int count[10] = { 0 };int start[10] = { 0 };//If you're dealing with a single digit, The count represents the number of data bits that appear in the//count corresponding position. 10 bits, similar to the hundred. for  (int i = 0; i < size; i++) {int num1 = a[i]  / divisor;count[num1 % 10]++;} for  (int j = 1; j < 10; j++) {start[j] =-digit, 10-bit, hundred, etc. &NBSP;START[J&NBSP;-&NBSP;1]&NBSP;+&NBSP;COUNT[J&NBSP;-&NBSP;1];} According to start, the data in a is placed in the TMP, ordered for  (int k = 0; k <  size; k++) {int num2 = a[k] / divisor;tmp[start[num2 % 10]++] = &NBSP;A[K];} Put the sorted data back into a for  (int n = 0; n < size; n++) {A[n] = tmp[n] ;}} Void radixsort (int *arr, int size) {assert (arr); int *tmp = new int[size ];int n = getmaxradix (arr, size);for  (int i = 1; i <=  n; i++) {int divisor = 1;int k = i;while  (--k) {divisor *=  10;} _radixsort (arr, size, divisor, tmp);} Delete[] tmp;}


Implementation of counting sort and cardinal sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.