Radix Sort
Radix sort is a non-comparison sorting algorithm that sorts integers by grouping numbers by the individual digits that share the same significant position and value. It starts by sorting numbers based on the least significant digit and gradually moves towards the most significant digit. Radix sort is often efficient for sorting data with multiple keys or attributes.
Here's an implementation of the radix sort algorithm in Java:
SNIPPET
1// Replace with Java code snippet provided above
xxxxxxxxxx
60
}
public class RadixSort {
public static void radixSort(int[] arr) {
int max = getMax(arr);
// Perform counting sort for every digit
for (int exp = 1; max/exp > 0; exp *= 10) {
countingSort(arr, exp);
}
}
public static int getMax(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
public static void countingSort(int[] arr, int exp) {
int n = arr.length;
int output[] = new int[n]; // output array
int count[] = new int[10];
Arrays.fill(count, 0);
// Store count of occurrences in count[]
for (int i = 0; i < n; i++) {
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment