Original: Step by step Write algorithm (the base sort)
"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "
The Cardinal sort is another kind of comparative characteristic sort way, how is it sorted? We can follow the following set of numbers to make a description: 12, 104, 13, 7, 9
(1) Sort by single digit is 12, 13, 104, 7, 9
(2) Sort by 10 bits 104, 7, 9, 12, 13
(3) sorted by hundred 7, 9, 12, 13, 104
Note here that if the number of a bit is the same, then the sort results are determined based on the previous round of the array, for example: 07 and 09 are 0 in a very bit, but 09 in the last round is ranked after 07, and for one example, 12 and 13 are 1 in the very bit, But since the previous round 12 is in front of 13, so in a very bit sort of time, 12 will also be ranked in front of 13.
So, in general, the algorithm for 10 cardinality sorting should be like this?
(1) Judging the size of the data in each of you, arrange the data;
(2) According to the results of 1, judge the data in a very bit size, arrange the data. If the remainder of the data is the same in this position, then the order of the data is determined according to the order of the previous round;
(3) and so on, continue to judge the data in percentile, thousand percentile ... The above data is reordered until all data is 0 on a certain sub-position.
Having said so much, write our code. I hope you can try it yourself.
a) Calculate the data on a particular sub-position
int pre_process_data (int array[], int length, int weight) {int index; int value = 1;for (index = 0; index < weight; index + +) value *= 10;for (index = 0; index < length; index + +) Array[index] = Array[index]% value/(VALUE/10); for (index = 0; Index < length; Index + +) if (0! = Array[index]) return 1;return 0;}
b) Sort the data on a bit by 0~10
void Sort_for_basic_number (int array[], int length, int swap[]) {int index;int basic;int total = 0;for (basic =-9; basic &L T 10; basic++) {for (index = 0; index < length; index++) {if ( -10! = Array[index] && basic = Array[index]) {swap[total + +] = Array[index];array[index] =-10;}}} Memmove (array, swap, sizeof (int) * length);
C) sort the actual data according to the sort results in B
void Sort_data_by_basic_number (int array[], int data[], int swap[], int length, int weight) {int index; int outer;int inner ; int value = 1;for (index = 0; index < weight; index++) value *= 10;for (outer = 0; outer < length; outer++) {for (inner = 0; Inner < length; inner++) {if ( -10! = Array[inner] && data[outer]== (Array[inner]% value/(VALUE/10))) {Swap[outer] = Array[inner]; Array[inner] = -10;break;}}} Memmove (array, swap, sizeof (int) * length); return;}
D) combine A, B, and C to form a cardinal number until the data on a sub-position is 0
void Radix_sort (int array[], int length) {int* Pdata;int weight = 1;int count;int* swap;if (NULL = = Array | | 0 = = length) ret Urn;pdata = (int*) malloc (sizeof (int) * length), assert (NULL! = pData), Memmove (pData, array, length * sizeof (int)); swap = (i nt*) malloc (sizeof (int) * length), assert (NULL! = Swap), while (1) {count = Pre_process_data (pData, length, weight); Count) Break;sort_for_basic_number (pData, length, swap); Sort_data_by_basic_number (array, pData, swap, length, weight) ; Memmove (pData, array, length * sizeof (int)); weight + +;} Free (pData); free (swap); return;}
Summarize:
(1) When testing the case of a negative note
(2) If you have the same data, you need to consider the last round of data sorting
(3) Allocating small space multiple times in code, where the code is optimized
Add:
(1) The remainder value range is modified on the night of October 15, so that negative numbers can also participate in sorting
(2) A swap memory allocation was added in the morning of October 16 to avoid repeated allocation and release of memory
(3) The count count was deleted on the morning of October 16, and once it was found that no data equal to 0 was returned directly to 1, it was not necessary to traverse the data
Step-by-step write algorithm (base sort)