Java Program to Implement Radix Sort

Problem Description

Write a Java Program to implement Radix Sort Algorithm.

What is Radix Sort in Java?
Radix sort is a sorting algorithm that sorts integers by their digits. It works by grouping numbers by each digit, starting from the least significant digit to the most significant digit. For each digit, the algorithm sorts the numbers into buckets based on the digit value. The buckets are then combined in order to create a sorted list of numbers.

In Java, Radix Sort can be implemented using arrays, queues, or LinkedLists to create the buckets. It has a linear time complexity and is often used to sort large integers, such as in cryptography or big data applications.

Program/Source Code

Here is the source code of the Java program to implement Radix Sort Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /*
  2.  * Java Program to Implement Radix Sort
  3.  */
  5. import java.util.Scanner;
  7. /** Class RadixSort **/
  8. public class RadixSort 
  9. {
  10.     /** Radix Sort function **/
  11.     public static void sort( int[] a)
  12.     {
  13.         int i, m = a[0], exp = 1, n = a.length;
  14.         int[] b = new int[n];
  15.         for (i = 1; i < n; i++)
  16.             if (a[i] > m)
  17.                 m = a[i];
  18.         while (m / exp > 0)
  19.         {
  20.             int[] bucket = new int[10];
  22.             for (i = 0; i < n; i++)
  23.                 bucket[(a[i] / exp) % 10]++;
  24.             for (i = 1; i < 10; i++)
  25.                 bucket[i] += bucket[i - 1];
  26.             for (i = n - 1; i >= 0; i--)
  27.                 b[--bucket[(a[i] / exp) % 10]] = a[i];
  28.             for (i = 0; i < n; i++)
  29.                 a[i] = b[i];
  30.             exp *= 10;        
  31.         }
  32.     }    
  33.     /** Main method **/
  34.     public static void main(String[] args) 
  35.     {
  36.         Scanner scan = new Scanner( );        
  37.         System.out.println("Radix Sort Test\n");
  38.         int n, i;
  39.         /** Accept number of elements **/
  40.         System.out.println("Enter number of integer elements");
  41.         n = scan.nextInt();
  42.         /** Create integer array on n elements **/
  43.         int arr[] = new int[ n ];
  44.         /** Accept elements **/
  45.         System.out.println("\nEnter "+ n +" integer elements");
  46.         for (i = 0; i < n; i++)
  47.             arr[i] = scan.nextInt();
  48.         /** Call method sort **/
  49.         sort(arr);
  50.         /** Print sorted Array **/
  51.         System.out.println("\nElements after sorting ");        
  52.         for (i = 0; i < n; i++)
  53.             System.out.print(arr[i]+" ");            
  54.         System.out.println();                     
  55.     }    
  56. }
Program Explanation

1. The program defines a class called RadixSort that contains a static method called sort, which takes an integer array as input.
2. The sort method begins by finding the maximum element in the input array to determine the number of digits in the largest number. It then initializes an empty array called b with the same length as the input array a.
3. The program then enters a loop that will iterate through each digit of the numbers in the array, starting from the least significant digit to the most significant digit.
4. Within each iteration, the program creates a new array called bucket with a length of 10.
5. The program iterates through the input array and increments the appropriate bucket based on the current digit being examined. It then modifies the bucket array to contain the running sum of elements in each bucket.
6. The program then iterates through the input array in reverse order and places each element into the appropriate position in the output array b based on the current digit being examined. 7. Finally, it copies the sorted elements from array b back into the original input array a.
8. The class also contains a main method that prompts the user to input the number of integers to sort, creates an integer array with that length, prompts the user to input the integers themselves, sorts the array using the sort method, and then prints out the sorted array.


Time Complexity: O(d * (n + k))
Radix Sort has time complexity O(d * (n + k)), where d is the number of digits in the maximum element, n is the number of elements to be sorted, and k is the range of input values. The implementation finds the max element in O(n) time and uses a bucket array of size 10, taking O(1) time per loop.

Space Complexity: O(n + k)
The space complexity of the implementation is O(n + k), where n is the size of the input array and k is the size of the bucket array. In this case, k is constant and equal to 10, so the space complexity reduces to O(n). Additionally, a new array of size n is created, which also contributes to the space complexity.

Program Output
Radix Sort Test
Enter number of integer elements
Enter 10 integer elements
877 567 3456 876 467 26 934 9876 1 4567
Elements after sorting
1 26 467 567 876 877 934 3456 4567 9876

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.