Skip to content

Latest commit

ย 

History

History
186 lines (116 loc) ยท 3.75 KB

File metadata and controls

186 lines (116 loc) ยท 3.75 KB

ํž™ ์†ŒํŠธ(Heap Sort)


์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ํ•˜๋Š” ํž™(Heap) ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœํ•œ ์ •๋ ฌ ๋ฐฉ์‹

์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ๋ž€?

์‚ฝ์ž…ํ•  ๋•Œ ์™ผ์ชฝ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ์ด์ง„ ํŠธ๋ฆฌ

ํž™ ์†ŒํŠธ๋Š” ๋ถˆ์•ˆ์ • ์ •๋ ฌ์— ์†ํ•จ

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

ํ‰๊ท  ์ตœ์„  ์ตœ์•…
ฮ˜(nlogn) ฮฉ(nlogn) O(nlogn)
๊ณผ์ •
  1. ์ตœ๋Œ€ ํž™์„ ๊ตฌ์„ฑ
  2. ํ˜„์žฌ ํž™ ๋ฃจํŠธ๋Š” ๊ฐ€์žฅ ํฐ ๊ฐ’์ด ์กด์žฌํ•จ. ๋ฃจํŠธ์˜ ๊ฐ’์„ ๋งˆ์ง€๋ง‰ ์š”์†Œ์™€ ๋ฐ”๊พผ ํ›„, ํž™์˜ ์‚ฌ์ด์ฆˆ ํ•˜๋‚˜ ์ค„์ž„
  3. ํž™์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ 1๋ณด๋‹ค ํฌ๋ฉด ์œ„ ๊ณผ์ • ๋ฐ˜๋ณต

๋ฃจํŠธ๋ฅผ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋กœ ๋Œ€์ฒด (11 โ†’ 4), ๋‹ค์‹œ ์ตœ๋Œ€ ํž™ ๊ตฌ์„ฑ

์ด์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ตœ๋Œ€ ๊ฐ’์„ ํ•˜๋‚˜์”ฉ ๋ฝ‘์•„๋‚ด๋ฉด์„œ ์ •๋ ฌํ•˜๋Š” ๊ฒƒ์ด ํž™ ์†ŒํŠธ

public void heapSort(int[] array) {
    int n = array.length;
    
    // max heap ์ดˆ๊ธฐํ™”
    for (int i = n/2-1; i>=0; i--){
        heapify(array, n, i); // 1
    }
    
    // extract ์—ฐ์‚ฐ
    for (int i = n-1; i>0; i--) {
        swap(array, 0, i); 
        heapify(array, i, 0); // 2
    }
}
1๋ฒˆ์งธ heapify

์ผ๋ฐ˜ ๋ฐฐ์—ด์„ ํž™์œผ๋กœ ๊ตฌ์„ฑํ•˜๋Š” ์—ญํ• 

์ž์‹๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ ๋ถ€๋ชจ๋…ธ๋“œ ๋น„๊ต

  • n/2-1๋ถ€ํ„ฐ 0๊นŒ์ง€ ์ธ๋ฑ์Šค๊ฐ€ ๋„๋Š” ์ด์œ ๋Š”?

    ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ ์ž์‹๋…ธ๋“œ (i2 + 1), ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ(i2 + 2)์ด๊ธฐ ๋•Œ๋ฌธ

2๋ฒˆ์งธ heapify

์š”์†Œ๊ฐ€ ํ•˜๋‚˜ ์ œ๊ฑฐ๋œ ์ดํ›„์— ๋‹ค์‹œ ์ตœ๋Œ€ ํž™์„ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•จ

๋ฃจํŠธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ง„ํ–‰(extract ์—ฐ์‚ฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด)

public void heapify(int array[], int n, int i) {
    int p = i;
    int l = i*2 + 1;
    int r = i*2 + 2;
    
    //์™ผ์ชฝ ์ž์‹๋…ธ๋“œ
    if (l < n && array[p] < array[l]) {
        p = l;
    }
    //์˜ค๋ฅธ์ชฝ ์ž์‹๋…ธ๋“œ
    if (r < n && array[p] < array[r]) {
        p = r;
    }
    
    //๋ถ€๋ชจ๋…ธ๋“œ < ์ž์‹๋…ธ๋“œ
    if(i != p) {
        swap(array, p, i);
        heapify(array, n, p);
    }
}

๋‹ค์‹œ ์ตœ๋Œ€ ํž™์„ ๊ตฌ์„ฑํ•  ๋•Œ๊นŒ์ง€ ๋ถ€๋ชจ ๋…ธ๋“œ์™€ ์ž์‹ ๋…ธ๋“œ๋ฅผ swapํ•˜๋ฉฐ ์žฌ๊ท€ ์ง„ํ–‰

ํ€ต์ •๋ ฌ๊ณผ ํ•ฉ๋ณ‘์ •๋ ฌ์˜ ์„ฑ๋Šฅ์ด ์ข‹๊ธฐ ๋•Œ๋ฌธ์— ํž™ ์ •๋ ฌ์˜ ์‚ฌ์šฉ๋นˆ๋„๊ฐ€ ๋†’์ง€๋Š” ์•Š์Œ.

ํ•˜์ง€๋งŒ ํž™ ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ๋งŽ์ด ํ™œ์šฉ๋˜๊ณ  ์žˆ์œผ๋ฉฐ, ์ด๋•Œ ํ•จ๊ป˜ ๋”ฐ๋ผ์˜ค๋Š” ๊ฐœ๋…์ด ํž™ ์†ŒํŠธ

ํž™ ์†ŒํŠธ๊ฐ€ ์œ ์šฉํ•  ๋•Œ
  • ๊ฐ€์žฅ ํฌ๊ฑฐ๋‚˜ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ๊ตฌํ•  ๋•Œ

    ์ตœ์†Œ ํž™ or ์ตœ๋Œ€ ํž™์˜ ๋ฃจํŠธ ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๋ฒˆ์˜ ํž™ ๊ตฌ์„ฑ์„ ํ†ตํ•ด ๊ตฌํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅ

  • ์ตœ๋Œ€ k ๋งŒํผ ๋–จ์–ด์ง„ ์š”์†Œ๋“ค์„ ์ •๋ ฌํ•  ๋•Œ

    ์‚ฝ์ž…์ •๋ ฌ๋ณด๋‹ค ๋”์šฑ ๊ฐœ์„ ๋œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์–ด๋‚ผ ์ˆ˜ ์žˆ์Œ

์ „์ฒด ์†Œ์Šค ์ฝ”๋“œ
private void solve() {
    int[] array = { 230, 10, 60, 550, 40, 220, 20 };
 
    heapSort(array);
 
    for (int v : array) {
        System.out.println(v);
    }
}
 
public static void heapify(int array[], int n, int i) {
    int p = i;
    int l = i * 2 + 1;
    int r = i * 2 + 2;
 
    if (l < n && array[p] < array[l]) {
        p = l;
    }
 
    if (r < n && array[p] < array[r]) {
        p = r;
    }
 
    if (i != p) {
        swap(array, p, i);
        heapify(array, n, p);
    }
}
 
public static void heapSort(int[] array) {
    int n = array.length;
 
    // init, max heap
    for (int i = n / 2 - 1; i >= 0; i--) {
        heapify(array, n, i);
    }
 
    // for extract max element from heap
    for (int i = n - 1; i > 0; i--) {
        swap(array, 0, i);
        heapify(array, i, 0);
    }
}
 
public static void swap(int[] array, int a, int b) {
    int temp = array[a];
    array[a] = array[b];
    array[b] = temp;
}