package study; // ì°¸ê³ : https://st-lab.tistory.com/104 public class CountingSort { public static void main(String[] args) { // ë°°ì´ 3ê° íì int[] array = new int[100]; // ì ë ¥ ê°ì : 100ê° int[] counting = new int[31]; // ìì ë²ì : 0 ~ 30 int[] result = new int[100]; // ì ë ¬ë ê²°ê³¼ // 1. array ë°°ì´ì ì ë ¥ë ì ì ì¥í기 for (int i = 0; i < array.length; i++) { array[i] = (int) (Math.random() * 31); // ì°ì° ê²°ê³¼ : 0 ~ 30 } // 2. counting ì ë ¬ array ë°°ì´ì ê°ì¼ë¡ ì´ê¸°í for (int i = 0; i < array.length; i++) { counting[array[i]]++; } // 3. counting ì ë ¬ì ëì í© êµ¬í기 for (int i = 1; i < counting.length; i++) { counting[i] += counting[i - 1]; } // 4. array ë°°ì´ì ë¤ìì ë¶í° ë°ë³µíë©° result ë°°ì´ì ì ë ¬ë ë°°ì´ ì ì¥í기 for (int i = array.length - 1; i >= 0; i--) { int value = array[i]; counting[value]--; // ê°ì ê°ì result[counting[value]] = value; } // 5. result ë°°ì´ ì¶ë ¥ (array ë°°ì´ì ì ë ¬ë ííê° ì ì¥ëì´ ìì) for (int i = 0; i < result.length; i++) { if (i % 10 == 0) System.out.println(); System.out.print(result[i] + " "); } System.out.println(); System.out.println(); // *** counting sort ì¶ì½í *** int[] counting_2 = new int[31]; // counting ì ë ¬ë§ ìì± for (int i = 0; i < 100; i++) { counting_2[(int)(Math.random() * 31)]++; } int count = 0; for (int i = 0; i < 31; i++) { if(counting_2[i] > 0) { // ê°ì´ íëë¼ë ì¡´ì¬íë¤ë©´ ë§ì¡± while(counting_2[i]-- >= 1) { if(count++ % 10 == 0) System.out.println(); System.out.print(i + " "); } } } } }