Radix Sort In Java

In this tutorial, we will figure out how to complete a radix sort in java with simple models.
Radix sort is the arranging calculation used to sort the numbers. Radix sort is otherwise called container sort or tallying sort.

Specific qualification for radix sort is that it makes a can or a bucket for every digit.

 

How to implement Radix Sort In Java

if you don’t know how to do a radix sort then you are at the right place. Let’s learn with simple examples.

It is the sorting algorithm used to sort the numbers in an ascending number. We sort the number from the least significant digit.

For example:-

On the off chance that we are managing unsorted numbers than we realize that there are 10 digits from 0 to 9 that are utilized to shape any number.

so we will require 10 cans named 0 to 9 to sort the unsorted numbers.

Let’s sort some unsorted numbers.

90,80,70,20,40,5,120,110

Since there are 10 digits from 0 to 9 so, we will need 10 buckets labeled 0 to 9 in order to sort the given unsorted numbers.

Now, find the number of digits in the biggest number.
100 is the biggest number and in this case, it has 3 digits.

So we will pad all the number that is smaller than 100 with leading zeros. It will look like:
090,080,070,020,040,005,120,110

since all of them have 3 digits so, the sorting process will require 3 passes.

pass1: -sort the numbers using the 1st digit from the right.
090 has 0 in 1st place so we will put it in bucket 0, similarly, 080 has 0 in 1st place so we will place it in bucket 0
070 has 0 in 1st place so we will put it in a bucket 0, 020 has 0 in 1st place so we will put it in bucket 0, 040 has 0 in 1st place so we will put it in bucket 0,and 005 has 5 in its 1st place so we will put it in bucket 5.

Pass 1 is complete. Take the numbers out from the bucket from 0 to 9 from the bottom.

Pass 1:-
090,080,070,020,040,120,110,005

Similarly,  we will do it for pass 2 but in pass 2 we will sort the numbers using 2nd digit from the right.

Pass 2:-005,110,020,120,040,070,080,090

Now we will do pass 3 where we will sort the numbers according to the last significant digit i.e., 3rd digit.

Pass 3:- 005,020,040,070,080,090,110,120

Remove the leading zeros.
The output will be 5,20,40,70,80,90,110,120

Java program to implement:

The below code will help you to achieve your goal:

import java.util.*;


class RadixSortExample {

  public static void main(String[] args) {

    System.out.println("Radix sort using Java");
    int[] numbers = { 10,15,1,60,5,100,25,50 };

    System.out.println("Before sorting the numbers are:-");
    System.out.println(Arrays.toString(numbers));

    radixs(numbers);

    System.out.println("Sorting an array using radix sort");
    System.out.println(Arrays.toString(numbers));

  }
public static void radixs(int[] numbers) {
    final int RADIX = 10;
    List<Integer>[] buck = new ArrayList[RADIX];
    
    for (int unique = 0; unique < buck.length(); unique++) {
      buck[unique] = new ArrayList<Integer>();
    }
boolean max = false;
    int temp = -1, p = 1;
    while (!max) 
     
      {
      max = true;
      
      for (Integer unique: numbers)
      {
        temp = unique/ p;
        buck[temp % RADIX].add(unique);
        if (max && temp > 0) {
          max = false;
        }
      }
      
     
      int Z = 0;
      for (z = 0;z < RADIX;z++) {
        for(Integer unique : buck[z]) {
          numbers[z++] = i;
        }
        buck[z].clear();
      }
      
     p=p*RADIX;
    }
  }
}

OUTPUT:

Radix sort using java
Before sorting the numbers are:-
10,15,1,60,5,100,25,50

sorting an array using radix sort
1,5,10,15,25,50,60,100

You may also read:

Leave a Reply

Your email address will not be published. Required fields are marked *