Next revision | Previous revision |
algorithm:radix_sort [2016/06/07 14:02] – 만듦 ledyx | algorithm:radix_sort [2021/02/07 03:15] (current) – external edit 127.0.0.1 |
---|
| |
= 문자열 = | = 문자열 = |
| |
| == 전체 문자열 == |
<sxh java> | <sxh java> |
public static String[] radixSort(String[] arr, int maxLen) { | public static String[] radixSort(String[] arr, int maxLen) { |
int bucketNum = 'z' - 'a' + 1; //문자열 비교 | int bucketNum = Character.MAX_VALUE; |
| |
| int charIndex = maxLen - 1; //최대길이 5이면 index 4부터 접근. |
| |
| Queue<String>[] buckets = new LinkedList[bucketNum]; |
| for (int j = 0; j < buckets.length; j++) |
| buckets[j] = new LinkedList(); // Queue 생성 |
| |
| for (int i = 0; i < maxLen; i++) { |
| |
| for (int digitIndex = 0; digitIndex < arr.length; digitIndex++) { |
| |
| try { |
| String str = arr[digitIndex]; |
| |
| if(str.length() != maxLen) { |
| throw new Exception("[" + digitIndex + "] 문자열 길이가 최대길이와 일치하지 않습니다."); |
| } |
| |
| int radix = str.charAt(charIndex); |
| |
| |
| buckets[radix].offer(arr[digitIndex]); |
| } catch (Exception e) { |
| System.err.println(e.getMessage()); |
| return null; |
| } |
| |
| } |
| |
| int pos = 0; |
| for (int bucketIndex = 0; bucketIndex < bucketNum; bucketIndex++) { |
| while (!buckets[bucketIndex].isEmpty()) { |
| arr[pos] = buckets[bucketIndex].poll(); |
| pos++; |
| } |
| } |
| |
| charIndex--; |
| } |
| |
| return arr; |
| } |
| </sxh> |
| |
| == 알파벳 == |
| <sxh java> |
| public static String[] radixSort(String[] arr, int maxLen) { |
| int bucketNum = 'z' - 'a' + 1; |
| |
int charIndex = maxLen - 1; //최대길이 5이면 index 4부터 접근. | int charIndex = maxLen - 1; //최대길이 5이면 index 4부터 접근. |