# ๊ธฐ์ˆ˜ ์ •๋ ฌ (Radix Sort)

written by sohyeon, hyemin ๐Ÿ’ก


# 1. ๊ธฐ์ˆ˜ ์ •๋ ฌ์ด๋ž€?

๊ธฐ์ˆ˜ ์ •๋ ฌ์€ ๋‚ฎ์€ ์ž๋ฆฌ์ˆ˜๋ถ€ํ„ฐ ๋น„๊ตํ•˜์—ฌ ์ •๋ ฌํ•ด ๊ฐ„๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ๋ณธ ๊ฐœ๋…์œผ๋กœ ํ•˜๋Š” ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.
๊ธฐ์ˆ˜ ์ •๋ ฌ์€ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์–ด๋–ค ๋น„๊ต ์—ฐ์‚ฐ๋„ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋Š” ์ƒ‰๋‹ค๋ฅธ ์ •๋ ฌ ๊ธฐ๋ฒ•์ด๋‹ค.


# 2. ๋™์ž‘ ๋ฐฉ์‹

10๊ฐœ์˜ ๋ฒ„์ผ“(bucket)์„ ๋งŒ๋“ค์–ด์„œ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ์ผ์˜ ์ž๋ฆฟ์ˆ˜๋ถ€ํ„ฐ ๊ฐ ์ž๋ฆฟ์ˆ˜์˜ ๊ฐ’์— ๋”ฐ๋ผ ๋ฒ„์ผ“์— ๋„ฃ๋Š”๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์œ„์—์„œ๋ถ€ํ„ฐ ์•„๋ž˜๋กœ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ฒ„์ผ“ ์•ˆ์— ๋“ค์–ด ์žˆ๋Š” ์ˆซ์ž๋“ค์„ ์ฝ์Œ์œผ๋กœ์จ ์ •๋ ฌ๋œ ์ˆซ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.


# 3. ํŠน์ง•

# 1) ์žฅ์ 

  • ์ž๋ฆฟ์ˆ˜๊ฐ€ ๊ณ ์ •๋˜์–ด ์žˆ์–ด ์•ˆ์ •์ ์ด๋‹ค.
  • ๋ฐ์ดํ„ฐ ๊ฐ„์˜ ์ƒ๋Œ€์  ์ˆœ์„œ๊ฐ€ ๋ณด์กด๋˜๋Š” stable ์ •๋ ฌ์ด๋‹ค.
  • ๋น„๊ต์—ฐ์‚ฐ์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค.

# 2) ๋‹จ์ 

  • ์ž๋ฆฟ์ˆ˜๊ฐ€ ์—†๋Š” ๋ฐ์ดํ„ฐ๋Š” ์ •๋ ฌํ•˜์ง€ ๋ชปํ•œ๋‹ค.
  • ์ถ”๊ฐ€์ ์ธ ์ €์žฅ ๊ณต๊ฐ„์ด ํ•„์š”ํ•˜๋‹ค.

# 4. ์‹œ๊ฐ„๋ณต์žก๋„

  • ๊ฐ€์žฅ ํฐ ๋ฐ์ดํ„ฐ์˜ ์ž๋ฆฌ ์ˆ˜ ๋งŒํผ ์•ˆ์ „ํ•œ ์ •๋ ฌ ๊ณผ์ •์ด ๋ฐ˜๋ณต๋œ๋‹ค.
RadixSort(A,d)
    for i=1 to d
        StableSort(A) on digit i
        
๋”ฐ๋ผ์„œ, T(n) = O(dn)

# 5. ๊ธฐ์ˆ˜ ์ •๋ ฌ Java ์ฝ”๋“œ

# ex) ์˜ˆ์ œ

// ๊ธฐ์ˆ˜ ์ •๋ ฌ 
import java.io.*; 
import java.util.*; 

class Radix { 
    // ๋ฐฐ์—ด arr์—์„œ ์ตœ๋Œ“๊ฐ’์„ ์–ป๊ธฐ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ
    static int getMax(int arr[], int n) { 
        int mx = arr[0]; 
        for (int i = 1; i < n; i++) 
        if (arr[i] > mx) 
            mx = arr[i]; 
        return mx; 
    } 

    // ๊ฐ ์ž๋ฆฟ์ˆ˜์˜ ๊ฐ’์— ๋”ฐ๋ผ ์ •๋ ฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ 
    static void countSort(int arr[], int n, int exp) { 
        int output[] = new int[n]; // ๊ฒฐ๊ณผ ๋ฐฐ์—ด 
        int i; 
        int count[] = new int[10]; 
        Arrays.fill(count,0); 

        // Store count of occurrences in count[] 
        for (i = 0; i < n; i++) 
            count[ (arr[i]/exp)%10 ]++; 

        for (i = 1; i < 10; i++) 
            count[i] += count[i - 1]; 

        for (i = n - 1; i >= 0; i--) { 
            output[count[ (arr[i]/exp)%10 ] - 1] = arr[i]; 
            count[ (arr[i]/exp)%10 ]--; 
        } 

        for (i = 0; i < n; i++) 
            arr[i] = output[i]; 
    } 

    // ๊ธฐ์ˆ˜ ์ •๋ ฌ 
    static void radixsort(int arr[], int n) { 
        // ์ตœ๋Œ“๊ฐ’์˜ ์ž๋ฆฟ์ˆ˜๋ฅผ ์ฐพ๋Š”๋‹ค.
        int m = getMax(arr, n);     
        for (int exp = 1; m/exp > 0; exp *= 10) 
            countSort(arr, n, exp); 
    } 

    // ๋ฐฐ์—ด์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ 
    static void print(int arr[], int n) { 
        for (int i=0; i<n; i++) 
            System.out.print(arr[i]+" "); 
    } 

    // ๊ธฐ์ˆ˜ ์ •๋ ฌ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฉ”์ธ
    public static void main (String[] args) { 
        int arr[] = {170, 45, 75, 90, 802, 24, 2, 66}; 
        int n = arr.length; 
        radixsort(arr, n);  
        print(arr, n); 
    } 
} 


# Reference & Additional Resources

  • C์–ธ์–ด๋กœ ์‰ฝ๊ฒŒ ํ’€์–ด์“ด ์ž๋ฃŒ ๊ตฌ์กฐ

  • Radix Sort

Last Updated: 12/5/2020, 11:36:44 AM