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:
XML-Based Injection in Spring
Java Program to Implement CountMinSketch
HandlerAdapters in Spring MVC
Send email with SMTPS (eg. Google GMail)
Java Program to Implement Interpolation Search Algorithm
Spring WebClient and OAuth2 Support
Java Program to Check if a Matrix is Invertible
Java Program to Find Minimum Element in an Array using Linear Search
Hướng dẫn Java Design Pattern – Mediator
How to Read a Large File Efficiently with Java
A Guide to Java HashMap
Java Streams vs Vavr Streams
Java Program to Implement DelayQueue API
New Features in Java 10
Java Program to Implement Iterative Deepening
Java Program to Show the Duality Transformation of Line and Point
Spring Boot - Rest Controller Unit Test
Spring REST API + OAuth2 + Angular
Java Program to Implement Selection Sort
Spring MVC Custom Validation
Java Program to Perform Partial Key Search in a K-D Tree
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
So sánh ArrayList và Vector trong Java
Removing all Nulls from a List in Java
Java Program to subtract two large numbers using Linked Lists
Convert Character Array to String in Java
Get the workstation name or IP
Comparing Long Values in Java
Java Program to Perform Finite State Automaton based Search
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Java Program to Permute All Letters of an Input String
Sorting in Java