Skip to content

Instantly share code, notes, and snippets.

@danielrobertson
Created July 10, 2016 23:16
Show Gist options
  • Save danielrobertson/c052858b69861a925816725b1d126aa0 to your computer and use it in GitHub Desktop.
Save danielrobertson/c052858b69861a925816725b1d126aa0 to your computer and use it in GitHub Desktop.
Radix Sort
void radixSort(int arr[], int maxDigits) {
int exp = 1;
for (int i = 0; i < maxDigits; i++) {
ArrayList bucketList[] = new ArrayList[10];
for (int k = 0; k < 10; k++) {
bucketList[k] = new ArrayList();
}
for (int j = 0; j < arr.length; j++) {
int number = (arr[j] / exp ) % 10;
bucketList[number].add(arr[j]);
}
exp *= 10;
int index = 0;
for (int k = 0; k < 10; k++) {
for (int h = 0; h < bucketList[k].size(); h++) {
arr[index++] = (int)bucketList[k].get(h);
}
}
}
System.out.println("Sorted numbers");
for(int i =0; i < arr.length; i++){
System.out.print(arr[i] +", ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment