Classic Sorting algorithm---cardinal sort

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.