Skip to content

Instantly share code, notes, and snippets.

@senoritadeveloper01
Created January 19, 2022 19:01
Show Gist options
  • Save senoritadeveloper01/cc294fed083611b10e310976e493e6e3 to your computer and use it in GitHub Desktop.
Save senoritadeveloper01/cc294fed083611b10e310976e493e6e3 to your computer and use it in GitHub Desktop.
Radix Sort Implementation in Java
private static void radixSort(int array[]) {
int max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
for (int i = 1; max / i > 0; i *= 10) {
countingSort(array, i);
}
}
private static void countingSort(int arr[], int place) {
int[] output = new int[arr.length + 1];
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max)
max = arr[i];
}
int[] count = new int[max + 1];
for (int i = 0; i < max; ++i) {
count[i] = 0;
}
for (int i = 0; i < arr.length; i++) {
count[(arr[i] / place) % 10]++;
}
for (int i = 1; i <= 10; i++) {
count[i] += count[i - 1];
}
for (int i = arr.length - 1; i >= 0; i--) {
output[count[(arr[i] / place) % 10] - 1] = arr[i];
count[(arr[i] / place) % 10]--;
}
for (int i = 0; i < arr.length; i++) {
arr[i] = output[i];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment