Created
January 24, 2018 12:05
-
-
Save keehyun2/6def9a02f5923151bc3c4a763cf32e84 to your computer and use it in GitHub Desktop.
radixSort 기수 정렬
This file contains hidden or 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[] result = arr ; | |
for (int place = 1; place <= 1000000000; place *= 10) { | |
result = countingSort(result, place); | |
} | |
for (int i = 0; i < arr.length; i++) { | |
arr[i] = result[i]; | |
} | |
} | |
int[] countingSort(int[] input, int place) { | |
int[] out = new int[input.length]; | |
int[] count = new int[10]; | |
for (int i = 0; i < input.length; i++) { | |
int digit = getDigit(input[i], place); | |
count[digit] += 1; | |
} | |
for (int i = 1; i < count.length; i++) { | |
count[i] += count[i - 1]; | |
} | |
for (int i = input.length - 1; i >= 0; i--) { | |
int digit = getDigit(input[i], place); | |
out[count[digit] - 1] = input[i]; | |
count[digit]--; | |
} | |
return out; | |
} | |
int getDigit(int value, int digitPlace) { | |
return ((value / digitPlace) % 10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment