Skip to content
Home » Java Programs » Java Program for Radix Sort

Java Program for Radix Sort

Simple radix sort implementation in java. In java programming, this is used to sort the data in a specific order. Java is a programming language that allows you to perform radix type. 

In java programming, this is used to sort the data in a specific order. To sort the elements in java programs, a variety of important methods were used in the industry. In this program we have executed the radix sort implementation in java using for loop.

Radix Sort in Java

Radix sort is a number sorting algorithm that ranks integers according to their digit positions. It works by calculating the place value of a number’s digits. 

It does not compare the numbers, unlike most other sorting algorithms such as Merge Sort, Insertion Sort, and Bubble Sort. 

The radix sort algorithm sorts one digit at a time, ensuring that numbers that occur before other numbers in the input array will appear in the end, sorted array in the same order; this makes radix sort a stable algorithm. This is sample program for radix sort implementation in java for beginners.

Flag Variable in Java

A flag variable is a variable that has only one value until a condition is true or false, at which point the variable’s value is changed. 

It’s a variable that allows you to manage the flow of a function or statement while it’s running, allowing you to check for particular conditions while it’s running.

In programming, the flag variable is used as a signal to inform the programme that a given condition has been met.

It usually serves as a boolean variable that indicates whether a condition is true or false. Flag values are very important for radix sort implementation in java.

For Loop to Execute Radix Sort

In Java, the “for” loop is an entry-controlled loop that allows a user to iteratively run a block of statement(s) for a set number of times. The test-condition specified inside the “for” loop determines the number of iterations. 

The Java “for” loop is one of the most simple Java loops to grasp. Here we have the executed the radix sort implementation in java for beginners.

Radix Sort Implementation in Java

//radix sort implementation in java
import java.util.ArrayList;
import java.util.List;
public class Main
{
public static void main(String[] args)
{
int[] num = {5,565,64,4343,778,6656,567,456,7};
radixsort(num);
for (int h : num)
System.out.print(h + " ");
}
public static void radixsort(int[] input)
{
List[] buckets = new ArrayList[10];
for (int i = 0; i < buckets.length; i++) {
buckets[i] = new ArrayList();
}
boolean flag = false;
int tmp = -1, divisor = 1;
while (!flag) {
flag = true;
for (Integer i : input) {
tmp = i / divisor;
buckets[tmp % 10].add(i);
if (flag && tmp > 0) {
flag = false;
}
}
int a = 0;
for (int b = 0; b < 10; b++) {
for (Integer i : buckets[b]) {
input[a++] = i;
}
buckets[b].clear();
}
divisor *= 10;
}
}
}

Output for radix sort implementation in java:
5 7 8 64 456 565 567 778 4343 6656 6678