Tag: Operation namespace Its output calculation code STD memory Lin
Brief description of counting sort
read a lot of other people write the count sort. See a long time have not read, got a long time finally found so simple unexpectedly spent a few hours, so write here, hope and I just start to learn the people will not detour .
A summary of the idea of counting sequencing:
Set the sorted array to a, and then store to B after sorting. C is a temporary array.
The so-called count, first of all by an array C[i] calculates the number of elements equal to I. This process only needs a single loop traversal to be able. On this basis. Calculates the number of elements that are less than or equal to I, and is a heavy loop. 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 circulation, can ensure that the same elements of the relative order of the same, which is the count of sorting stability. Stability is important when array a has attachment properties.
Second, brief description:
In fact. Counting sorting to waste a lot of memory space, and the sequence of negative numbers discussed here is also an impossible problem and the ordered sequence of books must be located between 0-k, but its algorithm complexity small O (n+k) is its algorithm complexity, the algorithm is a stable algorithm.
There are two situations in which we do sort. One is the sort of number that does not recur. There is one such situation.
The above-mentioned people do not understand, said the ordinary point is to give you an array such as a[10] = {2, 2, 3, 4,5,6,7,9,0,6} This array, we create a temporary array to save, is also the operation of this algorithm classic, that is, we create a c[k] such an array, Here k indicates that the condition is to use the condition that the algorithm must satisfy, so that the elements in array a must be between 0-k.
We use the array C subscript to record the elements in array a that we need to sort. Then just output a record of these subscript.
Assuming that there are no repeated sequences, we are very easy to get this sort of sequence of sequences such 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 removing the label sort |
0 |
|
2 |
|
4 |
|
6 |
7 |
|
9 |
then we put the sorted number into a new number b[5]={0,0,0,0,0,0}
Get:b[5] ={0,2,4,6,7,9} so we get this sequence.
Assume that there are repeated situations:
We just need to record a few iterations. The second line in the table above is used to solve the problem, and we can use it to record a few iterations. Thus achieving a thorough solution to this sort of problem.
This part of the code is posted here:
int j=0,temp; for (int i=0;i<=k;i++) {//In this non-repeating c[i] All is 1 temp=c[i];//feel 0 while (temp-->0) {//Here solve the recurring problem b[j++ ]=i;//i is the number in the sorted data }}
We use this array of C to record the same. That is, repeated occurrences of the number.
Paste all the code:
/*** count sequencing is a non-stable and efficient sorting algorithm * @author rookie * @version 2014.7.13*/#include <iostream> #include <malloc.h># Include <windows.h>using namespace std;//count algorithm function/*** @param int a[] to receive ordered sequences * @param int length_a Indicates the length of the sequence to be sorted *@ param int b[] to hold the sorted sequence * @param int k indicates that the number that needs to be arranged must be between 0-k * @return no */void countsort (int a[],int length_a,int b[],int k) { Received an array of required sorts to be passed in. And know that its value is between 0-k//At this point we need to create an array to temporarily save the data int *c =null; int temp; if (c!=null) {free (c);} c = (int *) malloc (sizeof (int) *k); if (c! = NULL) {cout<< "Space request succeeded! "<<endl;} At this point, you must first clear 0 for (int i = 0; i< k;i++) {c[i] = 0;}//Count the number of sorts and place it at the appropriate 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 in the sorted data}} free (c);} /*** output function * @param intarraynum[] means the array that accepts the output required * @param int length_num Indicates the length of the array to be output * @return no */void output (int arraynum[],int Length_num) {for (int i =0; i&lT length_num;i++) {cout<< "<<i+1<<" elements: "<<ARRAYNUM[I]<<ENDL;} cout<< "Output is complete. "<<endl;} int main () {///if the sequence of arrays to be sorted is int a[10] = {1,3,4,5,2,5,2,7,9,0}; int b[10] = {0}; int k = ten; 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 is validated.
Counting sort of basic sort series