Radix sort)

Source: Internet
Author: User

Reference: http://en.wikipedia.org/wiki/Radix_sort#Definition

Count sorting can be understood as multi-Keyword sorting. (Therefore, a stable sorting algorithm, such as counting sorting, is required during sorting)

It is implemented in this way: all the values to be compared (positive integers) are unified into the same digit length, and the number before the shorter digits is zero. Then, sort the data by bit. In this way, the sequence is changed to an ordered sequence after the ranking is completed until the sorting is completed by the highest bit.

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.

An example

Original, unsorted list:

170, 45, 75, 90,802, 24, 2, 66

Sorting by least significant digit (1 s place) gives: [* notice that we keep 802 before 2, because 802 occurred before 2 in the original list, and similarly for pairs 170 & 90 and 45 & 75.]

17 0, 9 0, 80 2, 2, 2 4, 4 5, 7 5, 6 6Single digit

Sorting by next digit (10 s place) gives: [* notice that 802 again comes before 2 as 802 comes before 2 in the previous list.]

8 02, 2, 24, 45, 66, 1 70, 75,
9
0 to 10 (ensure that 170 is in front of 90)

Sorting by most significant digit (100 s place) gives:

2, 24, 45, 66, 75, 90, 170, 8Bits
(That is, sort by single digit, then by ten digits, and by hundreds of digits. However, ensure the order of the "same" elements in the last sorting. Therefore, a stable Sorting Algorithm must be used. Such as counting and sorting)

I read the code on the Wiki, added some notes, and recorded the following:

/* Base sorting from wiki: Radix sort */# include <stdio. h> # define max 5 // # define showpassvoid print (int * a, int N) {int I; for (I = 0; I <n; I ++) printf ("% d \ t", a [I]);} void radixsort (int * a, int N) {int I, B [Max], M = A [0], exp = 1; // M = the largest number in the array. Used to control the number of cycles below. For (I = 0; I <n; I ++) {if (a [I]> m) M = A [I];} while (M/EXP> 0) {int bucket [10] = {0}; // clear bucket for (I = 0; I <n; I ++) bucket [A [I]/EXP % 10] ++; // A [I]/EXP % 10 this expression is a single digit for a [I] (exp = 1), 10 digits (exp = 10), 100 digits (exp = )...... // bucket [0 .. 9] count the numbers for (I = 1; I <10; I ++) bucket [I] + = bucket [I-1]; // The intention is to reflect 0 ~ 9. The weight increases. (More strictly speaking, it refers to the number of numbers smaller than itself, see Counting sort) for (I = n-1; I> = 0; I --) B [-- bucket [A [I]/EXP % 10] = A [I]; // according to the delimiter bit of a [I, find its ranking for (I = 0; I <n; I ++) A [I] = B [I]; exp * = 10; // compare the next position, or compare the next keyword # ifdef showpass printf ("\ npass:"); print (A, n); # endif} int main () {int arr [Max]; int I, n; printf ("enter total elements (n <% d):", max); scanf ("% d", & N ); printf ("Enter % d elements:", n); for (I = 0; I <n; I ++) scanf ("% d", & arr [I]); printf ("\ narray:"); print (& arr [0], n); radixsort (& arr [0], n); printf ("\ nsorted:"); print (& arr [0], n ); printf ("\ n"); Return 0;}/* condition macro: can be used as a switch. Turn on the switch during debugging and output more information to observe program running. After debugging, turn off the switch. 1) define the Macro during compilation and use the-D option of GCC. For details, see man gccgcc-wall radix_sort.c-D showpass2) or define the macro in the source file # define showpass */

Note: This Radix sort uses the conuting sort algorithm.

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.