This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| algorithm:radix_sort [2016/06/07 13:02] – 만듦 ledyx | algorithm:radix_sort [2021/02/07 03:15] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 41: | Line 41: | ||
| = 문자열 = | = 문자열 = | ||
| + | |||
| + | == 전체 문자열 == | ||
| <sxh java> | <sxh java> | ||
| public static String[] radixSort(String[] arr, int maxLen) { | public static String[] radixSort(String[] arr, int maxLen) { | ||
| - | int bucketNum = ' | + | int bucketNum = Character.MAX_VALUE; |
| + | |||
| + | int charIndex = maxLen - 1; // | ||
| + | |||
| + | Queue< | ||
| + | for (int j = 0; j < buckets.length; | ||
| + | buckets[j] = new LinkedList(); | ||
| + | |||
| + | 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(" | ||
| + | } | ||
| + | |||
| + | 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 java> | ||
| + | public static String[] radixSort(String[] arr, int maxLen) { | ||
| + | int bucketNum = ' | ||
| int charIndex = maxLen - 1; // | int charIndex = maxLen - 1; // | ||