Created
July 10, 2016 23:16
-
-
Save danielrobertson/c052858b69861a925816725b1d126aa0 to your computer and use it in GitHub Desktop.
Radix Sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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