Radix Sort
Radix sort processes the elements the same way in which the names of the students are sorted according to their alphabetical order. There are 26 radix in that case due to the fact that, there are 26 alphabets in English. In the first pass, the names are grouped according to the ascending order of the first letter of names.
In the second pass, the names are grouped according to the ascending order of the second letter. The same process continues until we find the sorted list of names. The bucket are used to store the names produced in each pass. The number of passes depends upon the length of the name with the maximum letter.
In the case of integers, radix sort sorts the numbers according to their digits. The comparisons are made among the digits of the number from LSB to MSB. The number of passes depend upon the length of the number with the most number of digits.
Complexity
Complexity | Best Case | Average Case | Worst Case |
---|---|---|---|
Time Complexity | Ω(n+k) | θ(nk) | O(nk) |
Space Complexity | O(n+k) |
Example
Consider the array of length 6 given below. Sort the array by using Radix sort.
A = {10, 2, 901, 803, 1024}
Pass 1: (Sort the list according to the digits at 0's place)
10, 901, 2, 803, 1024.
Pass 2: (Sort the list according to the digits at 10's place)
02, 10, 901, 803, 1024
Pass 3: (Sort the list according to the digits at 100's place)
02, 10, 1024, 803, 901.
Pass 4: (Sort the list according to the digits at 1000's place)
02, 10, 803, 901, 1024
Algorithm
- Step 1:Find the largest number in ARR as LARGE
- Step 2: [INITIALIZE] SET NOP = Number of digits
in LARGE - Step 3: SET PASS =0
- Step 4: Repeat Step 5 while PASS <= NOP-1
- Step 5: SET I = 0 and INITIALIZE buckets
- Step 6:Repeat Steps 7 to 9 while I
- Step 7: SET DIGIT = digit at PASSth place in A[I]
- Step 8: Add A[I] to the bucket numbered DIGIT
- Step 9: INCREMENT bucket count for bucket
numbered DIGIT
[END OF LOOP] - Step 10: Collect the numbers in the bucket
[END OF LOOP] - Step 11: END
C Program
- #include <stdio.h>
- int largest(int a[]);
- void radix_sort(int a[]);
- void main()
- {
- int i;
- int a[10]={90,23,101,45,65,23,67,89,34,23};
- radix_sort(a);
- printf("\n The sorted array is: \n");
- for(i=0;i<10;i++)
- printf(" %d\t", a[i]);
- }
- int largest(int a[])
- {
- int larger=a[0], i;
- for(i=1;i<10;i++)
- {
- if(a[i]>larger)
- larger = a[i];
- }
- return larger;
- }
- void radix_sort(int a[])
- {
- int bucket[10][10], bucket_count[10];
- int i, j, k, remainder, NOP=0, divisor=1, larger, pass;
- larger = largest(a);
- while(larger>0)
- {
- NOP++;
- larger/=10;
- }
- for(pass=0;pass<NOP;pass++) // Initialize the buckets
- {
- for(i=0;i<10;i++)
- bucket_count[i]=0;
- for(i=0;i<10;i++)
- {
- // sort the numbers according to the digit at passth place
- remainder = (a[i]/divisor)%10;
- bucket[remainder][bucket_count[remainder]] = a[i];
- bucket_count[remainder] += 1;
- }
- // collect the numbers after PASS pass
- i=0;
- for(k=0;k<10;k++)
- {
- for(j=0;j<bucket_count[k];j++)
- {
- a[i] = bucket[k][j];
- i++;
- }
- }
- divisor *= 10;
- }
- }
Output:
The sorted array is: 23 23 23 34 45 65 67 89 90 101
Java Program
- public class Radix_Sort {
- public static void main(String[] args) {
- int i;
- Scanner sc = new Scanner(System.in);
- int[] a = {90,23,101,45,65,23,67,89,34,23};
- radix_sort(a);
- System.out.println("\n The sorted array is: \n");
- for(i=0;i<10;i++)
- System.out.println(a[i]);
- }
- static int largest(inta[])
- {
- int larger=a[0], i;
- for(i=1;i<10;i++)
- {
- if(a[i]>larger)
- larger = a[i];
- }
- returnlarger;
- }
- static void radix_sort(inta[])
- {
- int bucket[][]=newint[10][10];
- int bucket_count[]=newint[10];
- int i, j, k, remainder, NOP=0, divisor=1, larger, pass;
- larger = largest(a);
- while(larger>0)
- {
- NOP++;
- larger/=10;
- }
- for(pass=0;pass<NOP;pass++) // Initialize the buckets
- {
- for(i=0;i<10;i++)
- bucket_count[i]=0;
- for(i=0;i<10;i++)
- {
- // sort the numbers according to the digit at passth place
- remainder = (a[i]/divisor)%10;
- bucket[remainder][bucket_count[remainder]] = a[i];
- bucket_count[remainder] += 1;
- }
- // collect the numbers after PASS pass
- i=0;
- for(k=0;k<10;k++)
- {
- for(j=0;j<bucket_count[k];j++)
- {
- a[i] = bucket[k][j];
- i++;
- }
- }
- divisor *= 10;
- }
- }
- }
Output:
The sorted array is: 23 23 23 34 45 65 67 89 90 101
C# Program
- using System;
- public class Radix_Sort {
- public static void Main()
- {
- int i;
- int[] a = {90,23,101,45,65,23,67,89,34,23};
- radix_sort(a);
- Console.WriteLine("\n The sorted array is: \n");
- for(i=0;i<10;i++)
- Console.WriteLine(a[i]);
- }
- static int largest(int[] a)
- {
- int larger=a[0], i;
- for(i=1;i<10;i++)
- {
- if(a[i]>larger)
- larger = a[i];
- }
- return larger;
- }
- static void radix_sort(int[] a)
- {
- int[,] bucket=new int[10,10];
- int[] bucket_count=new int[10];
- int i, j, k, remainder, NOP=0, divisor=1, larger, pass;
- larger = largest(a);
- while(larger>0)
- {
- NOP++;
- larger/=10;
- }
- for(pass=0;pass<NOP;pass++) // Initialize the buckets
- {
- for(i=0;i<10;i++)
- bucket_count[i]=0;
- for(i=0;i<10;i++)
- {
- // sort the numbers according to the digit at passth place
- remainder = (a[i]/divisor)%10;
- bucket[remainder,bucket_count[remainder]] = a[i];
- bucket_count[remainder] += 1;
- }
- // collect the numbers after PASS pass
- i=0;
- for(k=0;k<10;k++)
- {
- for(j=0;j<bucket_count[k];j++)
- {
- a[i] = bucket[k,j];
- i++;
- }
- }
- divisor *= 10;
- }
- }
- }
Output:
The sorted array is: 23 23 23 34 45 65 67 89 90 101