Suppose that there is a string of values as follows: 73, 22, 93, 43, 55, 14, 28, 65, 39, 81 first, based on the numeric value of single digits, they are assigned to a bucket numbered 0 through 9 when the value is visited: 01 812 223 73 93 434 145 55 65678 289 39 The second step is to re-string the values of these buckets into the following series: 81, 22, 73, 93, 43, 14, 55, 65, 28, 39 and then again, this time based on the 10-digit allocation: 01 142 22 283 394 435 556 657 738 819 93 The third step is to re-string the values in these buckets into the following series: 14, 22, 28, 39, 43, 55, 65, 73, 81, 93 code:
Gets the end number int getmantissa (int num, int digits) {int temp = 1;for (int x = 0; x < digits-1; ++x) temp *= 10;return (num/ Temp)% 10;} void Radixsort (struct sq_list *v) {int digit = 1;//Gets the number of bits int save[10][100001];for (int i = 0, p = ten; I < v->length; + +i) while (V->elem[i].id >= p) {p *= 10;++digit;} Initializes a two-bit array for each row of the first element for (int x = 0; x < × x + +) {save[x][0] = 0;} Cardinal sort for (int i = 1; i <= digit; i++) {for (int x = 0; x < v->length+1; ++x) {int mantissa = Getmantissa (v->e Lem[x].id, i); int index = ++save[mantissa][0]; The first dollar of each row stores the amount of data per row save[mantissa][index] = v->elem[x].id;} Merge for (int i = 0, x = 0; i < i++) {for (int j = 0; J < save[i][0]; ++j) v->elem[x++].id = save[i][j + 1];sa Ve[i][0] = 0; Reinitialize each line of cells}}}
Classic Sorting algorithm---cardinal sort