The base sorting method can be LSD (least significant digital) or MSD (most significant digital). The LSD sorting method starts from the rightmost of the key value, while MSD is the opposite, start from the leftmost of the key value. Take LSD as an example. Assume that there is a string of values as follows: 73, 22, 93, 43, 55, 14, 28, 65, 39, and 81. First, based on the single-digit value, assign these values to the buckets numbered 0 to 9 during the visit: 0 1 81 2 22 3 73 93 43 4 14 5 65 6 7 8 28 9 39 then concatenate the values in these barrels into the following series: 81, 22, 73, 93, 43, 14, 55, 65, 28, 39 and then perform another allocation. This allocation is based on ten digits: 0 1 14 2 22 28 3 39 4 43 5 55 6 65 7 73 8 81 9 93 then concatenate the values in these barrels into the following series: 14, 22, 28, 39, 43, 55, 65, 73, 81, 93 the entire series has been sorted. If the sorting object has more than three numbers, continues until the maximum number of digits. The base sorting of LSD applies to columns with small digits. If Multiple Digits exist, MSD is more efficient. MSD is the opposite of LSD. It is allocated from the base of the High-digit base, but it is not merged into an array immediately after the allocation, instead, create a "sub-bucket" in each "Bucket" and assign the values in each bucket to the "sub-bucket" based on the values of the next digit. After the minimum number of digits is allocated, the data is merged into a single array.
/** Content: Base sorting, which adopts the LSD strategy, that is, sorting from the low position, commonly known as bucket sorting * Time: 2/10/2013 */# include <stdio. h> # include <time. h> # include <stdlib. h> # define max 10 using namespace STD; void printarray (INT array []) // print the Array {for (INT I = 0; I <Max; ++ I) {cout <array [I] <"" <flush ;}cout <Endl ;}// 0 ~ Bucket 9, bucket sorting void algorithmossip (INT array []) // algorithmossip (base sorting) {int temp [10] [Max] = {0 }; // set the bucket size int order [Max] = {0}; // used to indicate the capacity of each bucket int bit = 1; // It is used to indicate int LSD = 0 such as Count, ten, and hundred; // It is used to store the number of digits, such as the int sortcount = 1 above the single digit; // used to calculate the total number of times the while (bit <11) times are sorted. // Based on the random number generation, 1 ~ is generated here ~ 99, so only a few or ten digits need to be processed {// put the array in the appropriate bucket according to the rule for (INT I = 0; I <Max; ++ I) {LSD = (array [I]/bit) % 10; temp [LSD] [order [LSD] = array [I]; // place the number in the appropriate bottom of the bucket + + order [LSD]; // because the element already exists in the previous position of the bucket, so we need to add 1} // sort the elements in the bucket according to the rules and then re-place them back to the original number Group Int arraycount = 0; // mark the position of the array for (INT I = 0; I <Max; ++ I) {If (Order [I]! = 0) // when an element exists in the bucket, the content is taken out {for (Int J = 0; j <order [I]; ++ J) {array [arraycount] = temp [I] [J]; ++ arraycount;} order [I] = 0; // empty bucket }}cout <"\ n no." <sortcount <"the result of the secondary sorting is as follows:" <Endl; ++ sortcount; printarray (array ); bit * = 10 ;}} int main () {srand (time (0); int data [Max] = {0}; cout <"Before sorting: <Endl; For (INT I = 0; I <Max; ++ I) {data [I] = rand () % 100;} printarray (data ); algorithmossip (data); the result of cout <"\ n sorting is as follows:" <Endl; printarray (data); return 1 ;}