Java Program to Implement Radix Sort

«
»
This is a Java Program to implement Radix Sort Algorithm. This program is to sort a list of numbers.
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.  **/
  4.  
  5. import java.util.Scanner;
  6.  
  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];
  21.  
  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( System.in );        
  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. }

advertisement
Radix Sort Test
 
Enter number of integer elements
10
 
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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

If you wish to look at all Java Programming examples, go to Java Programs.

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!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter