Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value. Radix sort dates back as far as 1887 to the work of Herman Hollerith on tabulating machines.

###

##

###
__Basic Steps To Be Performed:__

__Basic Steps To Be Performed:__

Each key is first figuratively dropped into one level of buckets corresponding to the value of the rightmost digit. Each bucket preserves the original order of the keys as the keys are dropped into the bucket. There is a one-to-one correspondence between the number of buckets and the number of values that can be represented by a digit. Then, the process repeats with the next neighboring digit until there are no more digits to process. In other words:

- Take the least significant digit of each key.
- Group the keys based on that digit, but otherwise keep the original order of keys.
- Repeat the grouping process with each more significant digit.

The sort in step 2 is usually done using bucket sort or counting sort, which are efficient in this case since there are usually only a small number of digits.

##
__Step-by-step example:__

__Step-by-step example:__

Original, unsorted list:

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

Sorting by least significant digit (1s place) gives:

- 17
__0__, 9__0__, 80__2__,__2__, 2__4__, 4__5__, 7__5__, 6__6__

Sorting by next digit (10s place) gives:

- 8
__0__2, 2,__2__4,__4__5,__6__6, 1__7__0,__7__5,__9__0

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

- 2, 24, 45, 66, 75, 90,
__1__70,__8__02

It is important to realize that each of the above steps requires just a single pass over the data, since each item can be placed in its correct bucket without having to be compared with other items.

Some LSD radix sort implementations allocate space for buckets by first counting the number of keys that belong in each bucket before moving keys into those buckets. The number of times that each digit occurs is stored in an array. Consider the previous list of keys viewed in a different way:

- 170, 045, 075,090, 002, 024, 802, 066

The first counting pass starts on the least significant digit of each key, producing an array of bucket sizes:

- 2 (bucket size for digits of 0: 17
__0__, 09__0__) - 2 (bucket size for digits of 2: 00
__2__, 80__2__) - 1 (bucket size for digits of 4: 02
__4__) - 2 (bucket size for digits of 5: 04
__5__, 07__5__) - 1 (bucket size for digits of 6: 06
__6__)

A second counting pass on the next more significant digit of each key will produce an array of bucket sizes:

- 2 (bucket size for digits of 0: 0
__0__2, 8__0__2) - 1 (bucket size for digits of 2: 0
__2__4) - 1 (bucket size for digits of 4: 0
__4__5) - 1 (bucket size for digits of 6: 0
__6__6) - 2 (bucket size for digits of 7: 1
__7__0, 0__7__5) - 1 (bucket size for digits of 9: 0
__9__0)

A third and final counting pass on the most significant digit of each key will produce an array of bucket sizes:

- 6 (bucket size for digits of 0:
__0__02,__0__24,__0__45,__0__66,__0__75,__0__90) - 1 (bucket size for digits of 1:
__1__70) - 1 (bucket size for digits of 8:
__8__02)

##
**C Program For Radix Sort:**

**C Program For Radix Sort:**/* Double-Click To Select Code */ #include<stdio.h> #include<conio.h> radix_sort(int array[], int n); void main() { int array[100],n,i; clrscr(); printf("Enter the number of elements to be sorted: "); scanf("%d",&n); printf("\nEnter the elements to be sorted: \n"); for(i = 0 ; i < n ; i++ ) { printf("\tArray[%d] = ",i); scanf("%d",&array[i]); } printf("\nArray Before Radix Sort:"); //Array Before Radix Sort for(i = 0; i < n; i++) { printf("%8d", array[i]); } printf("\n"); radix_sort(array,n); printf("\nArray After Radix Sort: "); //Array After Radix Sort for(i = 0; i < n; i++) { printf("%8d", array[i]); } printf("\n"); getch(); } radix_sort(int arr[], int n) { int bucket[10][5],buck[10],b[10]; int i,j,k,l,num,div,large,passes; div=1; num=0; large=arr[0]; for(i=0 ; i<n ; i++) { if(arr[i] > large) { large = arr[i]; } while(large > 0) { num++; large = large/10; } for(passes=0 ; passes<num ; passes++) { for(k=0 ; k<10 ; k++) { buck[k] = 0; } for(i=0 ; i<n ;i++) { l = ((arr[i]/div)%10); bucket[l][buck[l]++] = arr[i]; } i=0; for(k=0 ; k<10 ; k++) { for(j=0 ; j<buck[k] ; j++) { arr[i++] = bucket[k][j]; } } div*=10; } } }

##
*Output of Program:*

*Output of Program:*

**Please Comment If You Liked This Post !!**

This article uses material from the Wikipedia article Radix Sort which is released under the Creative Commons Attribution-Share-Alike License 3.0

Do you like this post? Please link back to this article by copying one of the codes below.

URL: HTML link code: Forum link code: