Brief count sorting
After reading the counting sorting by many others, I haven't understood it for a long time. After reading it for a long time, I finally found that it took a few hours to make it simple, so I wrote it here, I hope that beginners like me will not make any detour.
I. Briefly describe the idea of counting sorting:
Set the sorted array to A, and store it to B. C is a temporary array. The so-called count is to calculate the number of elements whose size is equal to I through an array C [I]. This process only requires one loop traversal. On this basis, calculating the number of elements smaller than or equal to I is also completed in a loop. The next step is the key: backward loop, from length [a] to 1, place a [I] to Position C [A [I] in B. The principle is: C [A [I] indicates the number of elements smaller than or equal to a [I], which is exactly where a [I] should be sorted. In addition, the reverse cycle from length [a] to 1 ensures that the relative order between the same elements remains unchanged, which also reflects the stability of counting sorting. When array A has an attachment attribute, stability is very important.
Ii. Brief description:
In fact, counting sorting wastes a lot of memory space, and the sequence of negative numbers cannot be resolved here, and the book of sorting sequence must be between 0 and K, however, O (N + k) is the complexity of the algorithm, which is a stable algorithm. There are two sort cases: one is the sorting of the number that does not appear repeatedly, and the other is the case.
The above is hard to understand. The common point is to give you an array like a [10] = {2, 2, 3, 4, 5, 6, 7, 9, 0, 6, we create a temporary array to save, which is also the classic operation of this algorithm. That is, we create an array like C [K, here, K indicates the condition that must be met when this algorithm is used, that is, the elements in array A must be between 0 and K. We use array C subscript to record the elements in array a that we need to sort, and then we only need to output the records of these subscripts. If there are no repeated sequences, we can easily obtain such sorted sequences as: A [5] = {2, 4, 6, 7, 9, 0}
| Subscript |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
| Array C |
1 |
0 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
1 |
| After sorting by subscript |
0 |
|
2 |
|
4 |
|
6 |
7 |
|
9 |
Then we put the sorted number into a new number B [5] = {0, 0, 0, 0}
We can get the sequence B [5] =.
If there are duplicates:
We only need to record several duplicates. The second row in the table above is used to solve this problem. We can record several duplicates, in this way, the Sorting Problem is completely solved.
This part of code is pasted here:
Int J = 0, temp; For (INT I = 0; I <= K; I ++) {// here, no duplicate C [I] contains all 1 temp = C [I]; // The default value is 0 while (temp --> 0) {// solve the duplicate problem here. B [J ++] = I; // I is the number of sorted data }}
We use the array C to record the same, that is, the number of repeated occurrences.
Paste all the code:
/*** Counting sorting is a non-comparative and stable and efficient sorting algorithm * @ author * @ version 2014.7.13 */# include <iostream> # include <malloc. h> # include <windows. h> using namespace STD; // counting algorithm function/*** @ Param int A [] is used to receive the sequence to be sorted * @ Param int length_a indicates the length of the sequence to be sorted * @ Param int B [] used to save the sorted sequence * @ Param int K indicates that the number to be sorted must be between 0 and K * @ return none */void countsort (int A [], int length_a, int B [], int K) {// receives the array to be sorted, and we know that the value is between 0 and K // at this time, we need to create an array to temporarily Save the data int * c = NULL; in T temp; If (C! = NULL) {free (c);} c = (int *) malloc (sizeof (INT) * k); If (C! = NULL) {cout <"Space Application successful! "<Endl ;}// you must first clear the array C for (INT I = 0; I <K; I ++) {c [I] = 0 ;}// count the number of sorting tasks and place them at the position of the corresponding value for (INT I = 0; I <length_a; I ++) {temp = A [I]; C [temp] ++;} Int J = 0; For (INT I = 0; I <= K; I ++) {temp = C [I]; while (temp --> 0) {B [J ++] = I; // I is the number of sorted data} Free (c );} /*** output function ** @ Param intarraynum [] indicates accepting the array to be output * @ Param int length_num indicates the length of the array to be output * @ return none */void output (int arraynum [], int length_num) {for (INT I = 0; I <length_num; I ++) {Cout <"no." <I + 1 <"element:" <arraynum [I] <Endl ;}cout <"output complete! "<Endl;} int main () {// assume that the array sequence to be sorted is int A [10] = }; int B [10] = {0}; int K = 10; cout <"unordered array sequence:" <Endl; output (A, 10 ); countsort (A, 10, B, 10); cout <"sorted array sequence:" <Endl; output (B, 10 ); system ("pause"); Return 0 ;}The above code has been verified!