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.

/**
 ** Java Program to Implement Radix Sort
 **/
 
import java.util.Scanner;
 
/** Class RadixSort **/
public class RadixSort 
{
    /** Radix Sort function **/
    public static void sort( int[] a)
    {
        int i, m = a[0], exp = 1, n = a.length;
        int[] b = new int[n];
        for (i = 1; i < n; i++)
            if (a[i] > m)
                m = a[i];
        while (m / exp > 0)
        {
            int[] bucket = new int[10];
 
            for (i = 0; i < n; i++)
                bucket[(a[i] / exp) % 10]++;
            for (i = 1; i < 10; i++)
                bucket[i] += bucket[i - 1];
            for (i = n - 1; i >= 0; i--)
                b[--bucket[(a[i] / exp) % 10]] = a[i];
            for (i = 0; i < n; i++)
                a[i] = b[i];
            exp *= 10;        
        }
    }    
    /** Main method **/
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner( System.in );        
        System.out.println("Radix Sort Test\n");
        int n, i;
        /** Accept number of elements **/
        System.out.println("Enter number of integer elements");
        n = scan.nextInt();
        /** Create integer array on n elements **/
        int arr[] = new int[ n ];
        /** Accept elements **/
        System.out.println("\nEnter "+ n +" integer elements");
        for (i = 0; i < n; i++)
            arr[i] = scan.nextInt();
        /** Call method sort **/
        sort(arr);
        /** Print sorted Array **/
        System.out.println("\nElements after sorting ");        
        for (i = 0; i < n; i++)
            System.out.print(arr[i]+" ");            
        System.out.println();                     
    }    
}

Output:

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

Related posts:

Java Program to Implement Best-First Search
Class Loaders in Java
Introduction to Spring Security Expressions
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Check If Two Lists are Equal in Java
Java Program to Implement Sorted List
Java Program to Implement Self Balancing Binary Search Tree
Java Program to Perform Naive String Matching
Java Program to Convert a Decimal Number to Binary Number using Stacks
4 tính chất của lập trình hướng đối tượng trong Java
Java Program to Implement DelayQueue API
Java Program to Implement Wagner and Fisher Algorithm for online String Matching
Tổng quan về ngôn ngữ lập trình java
Default Password Encoder in Spring Security 5
Exploring the New Spring Cloud Gateway
Instance Profile Credentials using Spring Cloud
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Using Spring @ResponseStatus to Set HTTP Status Code
Spring Boot Gradle Plugin
Spring Boot - Code Structure
Từ khóa throw và throws trong Java
Converting a Stack Trace to a String in Java
Java Program to Implement Range Tree
Transaction Propagation and Isolation in Spring @Transactional
Java Program to Implement Pollard Rho Algorithm
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Spring Webflux and CORS
Reading an HTTP Response Body as a String in Java
Debug a HttpURLConnection problem
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Quick Guide to Spring Bean Scopes