Radix sort is an integer-based linear sorting method that groups items depending on their location or radix. It sorts the components in each iteration by the value of the digit at the current place, from least significant to most significant.
The steps involved in radix sort are:
where, n is the number of elements to be sorted, and k is the maximum number of digits or characters in the input.
public static void radixSort(int[] arr) {
int n = arr.length;
int maxVal = getMaxValue(arr);
// Perform counting sort for each digit position
for (int exp = 1; maxVal / exp > 0; exp *= 10) {
countingSort(arr, n, exp);
}
}
private static int getMaxValue(int[] arr) {
int maxVal = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}
return maxVal;
}
private static void countingSort(int[] arr, int n, int exp) {
int[] count = new int[10];
int[] output = new int[n];
// Count the number of occurrences of each digit
for (int i = 0; i < n; i++) {
int digit = (arr[i] / exp) % 10;
count[digit]++;
}
// Calculate the prefix sum of count array
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
// Build the output array by placing elements in their sorted positions
for (int i = n - 1; i >= 0; i--) {
int digit = (arr[i] / exp) % 10;
output[count[digit] - 1] = arr[i];
count[digit]--;
}
// Copy the output array to the input array
for (int i = 0; i < n; i++) {
arr[i] = output[i];
}
}
The primary driver function that performs radix sort on the input array is the radixSort
method. It first uses the getMaxValue
to discover the largest value in the array, and then does a counting sort for each digit position, beginning with the least significant digit and progressing to the most significant digit. The countingSort
method is a helper function that performs counting sort on the input array for a specific digit position specified by the exp
parameter.
O(nlog(n))
time complexity and with the smallest space complexity possible.Example:
Input: array = {455, 61, 63, 45, 67, 135, 74, 49, 15, 5} Output: 5 15 45 49 61 63 67 74 135 455 Explanation: After sorting the array, the positions of some numbers are not changed (for example, 2 and 3), while the positions of other numbers are changed (for example, 1 and 5).
Solution:
import java.util.Arrays;
public class RadixSort {
public static void countingSort(int[] array, int sizeArray, int placeValue) {
final int base = 10;
int[] sortedArray = new int[sizeArray];
int[] count = new int[base];
for (int i = 0; i < sizeArray; i++) {
int digitAtPlaceValue = (array[i] / placeValue) % base;
count[digitAtPlaceValue]++;
}
for (int i = 1; i < base; i++) {
count[i] += count[i - 1];
}
for (int i = sizeArray - 1; i >= 0; i--) {
int digitAtPlaceValue = (array[i] / placeValue) % base;
int sortedIndex = count[digitAtPlaceValue] - 1;
sortedArray[sortedIndex] = array[i];
count[digitAtPlaceValue]--;
}
for (int i = 0; i < sizeArray; i++) {
array[i] = sortedArray[i];
}
}
public static void radixSort(int[] array, int sizeArray) {
int maxim = Arrays.stream(array).max().getAsInt();
for (int placeValue = 1; maxim / placeValue > 0; placeValue *= 10) {
countingSort(array, sizeArray, placeValue);
}
}
public static void main(String[] args) {
int[] array = {455, 61, 63, 45, 67, 135, 74, 49, 15, 5};
int sizeArray = array.length;
radixSort(array, sizeArray);
for (int i = 0; i < sizeArray; i++) {
System.out.print(array[i] + " ");
}
}
}
The radixSort
function first finds the maximum value in the array using Arrays.stream(array).max().getAsInt()
to determine the number of digits in the largest element, which will be used to determine the number of iterations of the countingSort
function. countingSort
takes three arguments: the unsorted array, its length, and the place value that represents the current digit being sorted sorts the array based on the digit at the current place value, using the Counting Sort algorithm.
Note: also read about Heap Sort
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…