Post

๐Ÿน 5. Sorting

10. ์ •๋ ฌ๊ณผ ํƒ์ƒ‰

1. ์„ ํƒ ์ •๋ ฌ(selection sort)

โ†’ ํ‰๊ท  ๋ฐ ์ตœ์•… ์‹คํ–‰ ์‹œ๊ฐ„: $O(n^2)$, ๋ฉ”๋ชจ๋ฆฌ: $O(1)$

์„ ํƒ์ •๋ ฌ์€ ์‹ฌํ”Œํ•˜์ง€๋งŒ ๋น„ํšจ์œจ์ ์ด๋‹ค.

[ ์ •๋ ฌ ๋ฐฉ๋ฒ• ]

  1. ๋ฐฐ์—ด์„ ์„ ํ˜• ํƒ์ƒ‰ํ•˜๋ฉฐ ๊ฐ€์žฅ ์ž‘์€ ์›์†Œ๋ฅผ ๋ฐฐ์—ด ๋งจ ์•ž์œผ๋กœ ๋ณด๋‚ธ๋‹ค.(๋งจ ์•ž์— ์žˆ๋˜ ์›์†Œ์™€ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊พผ๋‹ค.)
  2. ๊ทธ ๋‹ค์Œ์œผ๋กœ ๋‘ ๋ฒˆ์งธ๋กœ ์ž‘์€ ์›์†Œ๋ฅผ ์ฐพ์€ ๋’ค ์•ž์œผ๋กœ ๋ณด๋‚ด์ค€๋‹ค.
  3. ๋ชจ๋“  ์›์†Œ๊ฐ€ ์ •๋ ฌ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.

[ ์ตœ์„ ๊ณผ ์ตœ์•…์˜ ๊ฒฝ์šฐ ]

  • ์ตœ์„ ์˜ ๊ฒฝ์šฐ : ์ด๋ฏธ ์ •๋ ฌ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค
  • ์ตœ์•…์˜ ๊ฒฝ์šฐ : ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์„ ํƒ ์ •๋ ฌ์€ ์ตœ์„ ์˜ ๊ฒฝ์šฐ์™€ ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ชจ๋‘ ํƒ์ƒ‰์˜ ํšŸ์ˆ˜๋Š” $N^2$ ์ด๋ฏ€๋กœ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋ชจ๋‘ $O(N^2)$์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š”๋‹ค.

[ ์ฝ”๋“œ ]

1
2
3
4
5
6
7
8
9
10
11
12
13
void selectionSort(int[] arr) {
    for (int i = 0; i < N - 1; i++) {
        int minIndex = i;
        for (int j = i + 1; j < N; j++) {
            if (arr[minIndex] > arr[j]) {
                minIndex = j;
            }
        }
        int temp = arr[minIndex];
        arr[minIndex] = arr[i];
        arr[i] = temp;
    }
}

2. ์‚ฝ์ž… ์ •๋ ฌ(insertion sort)

โ†’ ํ‰๊ท  ๋ฐ ์ตœ์•…์˜ ์‹คํ–‰ ์‹œ๊ฐ„: $O(N^2)$, ๋ฉ”๋ชจ๋ฆฌ: $O(1)$

๋ณดํ†ต์˜ ๊ฒฝ์šฐ ์‚ฝ์ž… ์ •๋ ฌ๋ณด๋‹ค ํ€ต ์ •๋ ฌ์ด ํšจ์œจ์ ์ด๋‚˜ ์ •๋ ฌ์ด ๊ฑฐ์˜ ๋˜์–ด ์žˆ๋Š” ์ƒํ™ฉ์—์„œ๋Š” ํ€ต ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋ณด๋‹ค ๋” ๊ฐ•๋ ฅํ•˜๋‹ค.

[ ์ •๋ ฌ ๋ฐฉ๋ฒ• ]

  1. ๋ฐฐ์—ด์„ ์„ ํ˜• ํƒ์ƒ‰ํ•˜๋ฉฐ ํ˜„์žฌ index๊ฐ€ index ์•ž๊นŒ์ง€์˜ ๋ถ€๋ถ„์—์„œ ์‚ฝ์ž…๋  ์œ„์น˜๋ฅผ ์ฐพ์•„ ์‚ฝ์ž…ํ•œ๋‹ค.
  2. index๋ฅผ 1์”ฉ ํ‚ค์šด๋‹ค.
  3. ๋ชจ๋“  ์›์†Œ๊ฐ€ ์ •๋ ฌ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.

[ ์ตœ์„ ๊ณผ ์ตœ์•…์˜ ๊ฒฝ์šฐ ]

  • ์ตœ์„ ์˜ ๊ฒฝ์šฐ : ์ด๋ฏธ ์ •๋ ฌ์ด ๋˜์–ด์žˆ๋Š” ๊ฒฝ์šฐ โ†’ $O(N)$

    ํ•œ ๋ฒˆ์”ฉ๋ฐ–์— ๋น„๊ต๋ฅผ ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— $O(N)$

  • ์ตœ์•…์˜ ๊ฒฝ์šฐ : ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ๊ฒฝ์šฐ โ†’ $O(N^2)$

[ ์ฝ”๋“œ ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void insertionSort(int[] arr) {
    for (int i = 1; i < N; i++) {
        int temp = arr[i];
        int prev = i - 1;
        for (j = i - 1; j >= 0; j++) {
            if (arr[j] > temp) {
                arr[j+1] = arr[j];
                prev = j;
            } else {
                break;
            }
        arr[prev] = temp;
    }
}

3. ํž™ ์ •๋ ฌ(heap sort)

โ†’ ํ‰๊ท  ๋ฐ ์ตœ์•…์˜ ์‹คํ–‰ ์‹œ๊ฐ„: $O(NlogN)$, ๋ฉ”๋ชจ๋ฆฌ: $O(1)$

์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ $O(NlogN)$์ด๊ธด ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” ๋™์ผํ•œ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„ ๋‹ค๋ฅธ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜(ex. Quick sort)๋ณด๋‹ค ๋Š๋ฆฌ๋‹ค.

[ ์ž๋ฃŒ๊ตฌ์กฐ ํž™(Heap)์ด๋ž€? ]

: ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ์˜ ์ผ์ข…์œผ๋กœ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ์ž๋ฃŒ๊ตฌ์กฐ

์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’๋“ค ์ค‘์—์„œ ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์„ ๋น ๋ฅด๊ฒŒ ์ฐพ์•„๋‚ด๋„๋ก ๋งŒ๋“ค์–ด์ง„ ์ž๋ฃŒ๊ตฌ์กฐ

  • ํž™์€ ๋ฐ˜์ •๋ ฌ ์ƒํƒœ(๋Š์Šจํ•œ ์ƒํƒœ)๋ฅผ ์œ ์ง€ํ•œ๋‹ค
    • ํฐ ๊ฐ’์ด ์ƒ์œ„ ๋ ˆ๋ฒจ์— ์žˆ๊ณ , ์ž‘์€ ๊ฐ’์ด ํ•˜์œ„ ๋ ˆ๋ฒจ์— ์žˆ์Œ
    • ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ํ‚ค ๊ฐ’์ด ์ž์‹ ๋…ธ๋“œ์˜ ํ‚ค ๊ฐ’๋ณด๋‹ค ํ•ญ์ƒ ํฐ(์ž‘์€) ์ด์ง„ ํŠธ๋ฆฌ
  • ์ค‘๋ณต๋œ ๊ฐ’์„ ํ—ˆ์šฉํ•œ๋‹ค.

[ ํž™(heap) ๊ตฌํ˜„ ]

  • ํž™์„ ์ €์žฅํ•˜๋Š” ํ‘œ์ค€์ ์ธ ์ž๋ฃŒ๊ตฌ์กฐ โ†’ ๋ฐฐ์—ด
  • ๊ตฌํ˜„์„ ์‰ฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค์ธ 0์€ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.
  • ํŠน์ • ์œ„์น˜์˜ ๋…ธ๋“œ ๋ฒˆํ˜ธ๋Š” ์ƒˆ๋กœ์šด ๋…ธ๋“œ๊ฐ€ ์ถ”๊ฐ€๋˜์–ด๋„ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค.

    ex) ๋ฃจํŠธ ๋…ธ๋“œ์˜ ์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ๋Š” ํ•ญ์ƒ 3์ด๋‹ค.

  • ํž™์—์„œ์˜ ๋ถ€๋ชจ ๋…ธ๋“œ์™€ ์ž์‹ ๋…ธ๋“œ์˜ ๊ด€๊ณ„
    • ์™ผ์ชฝ ์ž์‹์˜ ์ธ๋ฑ์Šค = (๋ถ€๋ชจ์˜ ์ธ๋ฑ์Šค) * 2
    • ์˜ค๋ฅธ์ชฝ ์ž์‹์˜ ์ธ๋ฑ์Šค = (๋ถ€๋ชจ์˜ ์ธ๋ฑ์Šค) * 2 + 1
    • ๋ถ€๋ชจ์˜ ์ธ๋ฑ์Šค = (์ž์‹์˜ ์ธ๋ฑ์Šค) / 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class Heap<E> {

    private final Comparator<? super E> comparator;
    private static final int DEFAULT_CAPACITY = 10;

    private int size;  // ์š”์†Œ ๊ฐœ์ˆ˜

    private Object[] array;  // ์š”์†Œ๋ฅผ ๋‹ด์„ ๋ฐฐ์—ด

    // ์ƒ์„ฑ์ž Type 1 (์ดˆ๊ธฐ ๊ณต๊ฐ„ ํ• ๋‹น X)
    public Heap() {
        this(null);
    }

    public Heap(Comparator<? super E> comparator) {
        this.array = new Object[DEFAULT_CAPACITY];
        this.size = 0;
        this.comparator = comparator;
    }

    // ๋ฐ›์€ ์ธ๋ฑ์Šค์˜ ๋ถ€๋ชจ ๋…ธ๋“œ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜
    private int getParent(int index) {
        return index / 2;
    }

    // ๋ฐ›์€ ์ธ๋ฑ์Šค์˜ ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜
    private int getLeftChild(int index) {
        return index * 2;
    }

    // ๋ฐ›์€ ์ธ๋ฑ์Šค์˜ ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜
    private int getRightChild(int index) {
        return index * 2 + 1;
    }
}

[ ํž™(heap)์˜ ์‚ฝ์ž… ]

  1. ํž™์— ์ƒˆ๋กœ์šด ์š”์†Œ๊ฐ€ ๋“ค์–ด์˜ค๋ฉด, ์ผ๋‹จ ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ํž™์˜ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์— ์ด์–ด์„œ ์‚ฝ์ž…ํ•œ๋‹ค.
  2. ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ๋ถ€๋ชจ ๋…ธ๋“œ๋“ค๊ณผ ๊ตํ™˜ํ•ด์„œ ํž™์˜ ์„ฑ์งˆ์„ ๋งŒ์กฑ์‹œํ‚จ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
/* ์ตœ๋Œ€ํž™ ์‚ฝ์ž… */
void insertMaxHeap(int x) {
    maxHeap[++heapSize] = x; // ํž™ ํฌ๊ธฐ ํ•˜๋‚˜๋ฅผ ์ฆ๊ฐ€ํ•˜๊ณ  ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์— x๋ฅผ ๋„ฃ๋Š”๋‹ค
    
    for (int i = heapSize; i > 1; i/=2) {
        // ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๊ฐ€ ์ž์‹ ์˜ ๋ถ€๋ชจ ๋…ธ๋“œ๋ณด๋‹ค ํฌ๋ฉด swap
        if (maxHeap[i/2] < maxHeap[i]) {
            swap(i/2, i);
        } else {
            break;
        }
    }
}

[ ํž™(heap)์˜ ์‚ญ์ œ ]

  1. ์ตœ๋Œ€ ํž™์—์„œ ์ตœ๋Œ“๊ฐ’์€ ๋ฃจํŠธ ๋…ธ๋“œ์ด๋ฏ€๋กœ ๋ฃจํŠธ ๋…ธ๋“œ๊ฐ€ ์‚ญ์ œ๋œ๋‹ค.

    ์ตœ๋Œ€ ํž™์—์„œ ์‚ญ์ œ ์—ฐ์‚ฐ์€ ์ตœ๋Œ“๊ฐ’์„ ๊ฐ€์ง„ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

  2. ์‚ญ์ œ๋œ ๋ฃจํŠธ ๋…ธ๋“œ์—๋Š” ํž™์˜ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
  3. ํž™์„ ์žฌ๊ตฌ์„ฑํ•œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* ์ตœ๋Œ€ํž™ ์‚ญ์ œ */
int deleteMaxHeap() {
    if (heapSize == 0) { //๋ฐฐ์—ด์ด ๋นˆ ๊ฒฝ์šฐ
            return 0;
    }
    
    int item = maxHeap[1]; // ๋ฃจํŠธ ๋…ธ๋“œ์˜ ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค. 
    maxHeap[1] = maxHeap[heapSize]; // ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜ ๊ฐ’์„ ๋ฃจํŠธ ๋…ธ๋“œ์— ๋„ฃ๋Š”๋‹ค.
    maxHeap[heapSize--] = 0; // ํž™ ํฌ๊ธฐ๋ฅผ ํ•˜๋‚˜ ์ค„์ด๊ณ  ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. 

    for (int i = 1; i*2 <= heapSize; ) {
        // ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๊ฐ€ ์™ผ์ชฝ ๋…ธ๋“œ์™€ ์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ๋ณด๋‹ค ํฌ๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ๋‚˜๊ฐ„๋‹ค.
        if (maxHeap[i] > maxHeap[i*2] && maxHeap[i] > maxHeap[i*2]) {
            break;
        }
        // ์™ผ์ชฝ ๋…ธ๋“œ๊ฐ€ ๋” ํฐ ๊ฒฝ์šฐ, ์™ผ์ชฝ ๋…ธ๋“œ์™€ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ swap
        else if (maxHeap[i*2] > maxHeap[i*2+1] {
            swap(i, i*2);
            i = i*2;
        }
        // ์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ๊ฐ€ ๋” ํฐ ๊ฒฝ์šฐ, ์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ์™€ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ swap
        else {
            swap(i, i*2+1);
            i = i*2+1;
        }
    }
    return item;
}

[ ์ตœ์„ ๊ณผ ์ตœ์•…์˜ ๊ฒฝ์šฐ ]

ํž™ ์ •๋ ฌ์€ ์ตœ์„ , ํ‰๊ท , ์ตœ์•… ๋ชจ๋‘ O(NlogN)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค.

4. ํ€ต ์ •๋ ฌ(quick sort)

โ†’ ํ‰๊ท  ์‹คํ–‰ ์‹œ๊ฐ„: $O(NlogN)$, ์ตœ์•… ์‹คํ–‰ ์‹œ๊ฐ„: $O(N^2)$, ๋ฉ”๋ชจ๋ฆฌ: $O(logN)$

์ตœ์•…์˜ ๊ฒฝ์šฐ ์‹คํ–‰์‹œ๊ฐ„์ด ํž™ ์ •๋ ฌ๋ณด๋‹ค ๋Š๋ฆฌ์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ ํ€ต ์ •๋ ฌ์„ ์‚ฌ์šฉํ•œ๋‹ค.

ํž™ ์ •๋ ฌ๊ณผ ํ€ต ์ •๋ ฌ ๋ชจ๋‘ swap์ด ์‚ฌ์šฉ๋˜๋Š”๋ฐ ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ๋งŽ์„ ๋•Œ ํž™ ์ •๋ ฌ์ด ํ›จ์”ฌ ๋” ๋งŽ์€ swap์ด ์‹คํ–‰๋˜๋ฉด์„œ ์ตœ์•…์˜ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ํ€ต ์ •๋ ฌ์ด ๋” ๋น ๋ฅด๋‹ค.

[ ์ •๋ ฌ ๋ฐฉ๋ฒ• ]

  1. ๋ฌด์ž‘์œ„๋กœ ์„ ์ •๋œ ํ•œ ์›์†Œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์„ ๋ถ„ํ• ํ•œ๋‹ค.
  2. ์„ ์ •๋œ ์›์†Œ๋ณด๋‹ค ์ž‘์€ ์›์†Œ๋“ค์€ ์•ž์—, ํฐ ์›์†Œ๋“ค์€ ๋’ค๋กœ ๋ณด๋‚ธ๋‹ค.

    ๋ฐฐ์—ด ๋ถ„ํ•  ์ž‘์—…์€ ์—ฐ์†๋œ swap ์—ฐ์‚ฐ์„ ํ†ตํ•ด ํšจ์œจ์ ์œผ๋กœ ์ˆ˜ํ–‰๋œ๋‹ค

๋ฐฐ์—ด๊ณผ ๊ทธ ๋ถ€๋ถ„ ๋ฐฐ์—ด์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ๋ถ„ํ• ํ•ด ๋‚˜๊ฐ€๋ฉด ๊ฒฐ๊ตญ์— ๋ฐฐ์—ด์€ ์ •๋ ฌ๋œ ์ƒํƒœ์— ๋„๋‹ฌํ•œ๋‹ค.

ํ•˜์ง€๋งŒ ๋ฐฐ์—ด ๋ถ„ํ• ์— ์‚ฌ์šฉ๋˜๋Š” ์›์†Œ๊ฐ€ ์ค‘๊ฐ„๊ฐ’, ์ ์–ด๋„ ์ค‘๊ฐ„๊ฐ’์— ๊ฐ€๊นŒ์šด ๊ฐ’์ด ๋˜๋ฆฌ๋ผ๋Š” ๋ณด์žฅ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์—, ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋Š๋ฆฌ๊ฒŒ ๋™์ž‘ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

[ ์ตœ์„ ๊ณผ ์ตœ์•…์˜ ๊ฒฝ์šฐ ]

  • ์ตœ์•…์˜ ๊ฒฝ์šฐ : pivot ๊ฐ’์ด ์ตœ์†Œ๋‚˜ ์ตœ๋Œ€๊ฐ’์œผ๋กœ ์ง€์ •๋˜์–ด ํŒŒํ‹ฐ์…˜์ด ๋‚˜๋ˆ„์–ด์ง€์ง€ ์•Š์•˜์„ ๋•Œ โ†’ $O(N^2)$

[ ์ฝ”๋“œ ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void quickSort(int[] arr, int left, int right) {
    int index = partition(arr, left, right);
    if (left < index - 1) { // ์™ผ์ชฝ ์ ˆ๋ฐ˜ ์ •๋ ฌ
        quickSort(arr, left, index - 1);
    }
    if (index < right) { // ์˜ค๋ฅธ์ชฝ ์ ˆ๋ฐ˜ ์ •๋ ฌ 
        quickSort(arr, index, right);
    }
}

int partition(int[] arr, int left, int right) {
    int pivot = arr[(left + right) / 2]; // ๋ถ„ํ•  ๊ธฐ์ค€ ์›์†Œ ์„ ์ •
    while (left <= right) {
        // ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์˜ฎ๊ฒจ์•ผ ํ•˜๋Š” ์›์†Œ ํƒ์ƒ‰
        while (arr[left] < pivot) left++;

        // ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ์˜ฎ๊ฒจ์•ผ ํ•˜๋Š” ์›์†Œ ํƒ์ƒ‰
        while (arr[right] > pivot) right--;

        // ์›์†Œ๋ฅผ ์Šค์™‘ํ•œ ๋’ค left์™€ right๋ฅผ ์ด๋™
        if (left <= right) {
            swap(arr, left, right); // ์Šค์™‘
            left++;
            right--;
        }
    }
    return left;
}

5. ๋ณ‘ํ•ฉ ์ •๋ ฌ(merge sort)

โ†’ ํ‰๊ท  ๋ฐ ์ตœ์•… ์‹คํ–‰ ์‹œ๊ฐ„: $O(NlogN)$, ๋ฉ”๋ชจ๋ฆฌ: ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ค๋ฆ„

๋ฐฐ์—ด์„ ๋ถ„ํ• ํ–ˆ๋‹ค๊ฐ€ โ€˜๋ณ‘ํ•ฉโ€™์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณผ์ •์ด ๊ฐ€์žฅ ๋ณต์žกํ•˜๋‹ค.

[ ๋ณ‘ํ•ฉ ์ž‘์—… ]

  1. ๋ณ‘ํ•ฉ ๋Œ€์ƒ์ด ๋˜๋Š” ๋ฐฐ์—ด์˜ ๋‘ ๋ถ€๋ถ„์„ ์ž„์‹œ ๋ฐฐ์—ด(helper)์— ๋ณต์‚ฌํ•œ๋‹ค.
  2. ์™ผ์ชฝ ์ ˆ๋ฐ˜์˜ ์‹œ์ž‘ ์ง€์ (helperLeft)์™€ ์˜ค๋ฅธ์ชฝ ์ ˆ๋ฐ˜์˜ ์‹œ์ž‘ ์ง€์ (helperRight)๋ฅผ ์ถ”์ ํ•œ๋‹ค.
  3. helper๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๋‘ ๋ฐฐ์—ด์—์„œ ๋” ์ž‘์€ ๊ฐ’์˜ ์›์†Œ๋ฅผ ๊บผ๋‚ด์–ด ์›๋ž˜ ๋ฐฐ์—ด์— ๋ณต์‚ฌํ•ด ๋„ฃ๋Š”๋‹ค.
  4. ๋‘ ๋ฐฐ์—ด ์ค‘ ํ•œ ๋ฐฐ์—ด์— ๋Œ€ํ•œ ์ˆœํšŒ๊ฐ€ ๋๋‚œ ๊ฒฝ์šฐ์—๋Š” ๋‹ค๋ฅธ ๋ฐฐ์—ด์˜ ๋‚จ์€ ๋ถ€๋ถ„์„ ์›๋ž˜ ๋ฐฐ์—ด์— ๋‚จ๊น€์—†์ด ๋ณต์‚ฌํ•ด ๋„ฃ๊ณ  ์ž‘์—…์„ ๋งˆ์นœ๋‹ค.

[ ์ฝ”๋“œ ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
void mergesort(int[] array) {
    int[] helper = new int[array.length];
    mergesort(array, helper, 0, arraay.length - 1);
}

void mergesort(int[] array, int[] helper, int low, int high) {
    if (low < high) {
        int middle = (low + high) / 2;
        mergesort(array, helper, low, middle); // ์™ผ์ชฝ ์ ˆ๋ฐ˜ ์ •๋ ฌ
        mergesort(array, helper, middle+1, high); // ์˜ค๋ฅธ์ชฝ ์ ˆ๋ฐ˜ ์ •๋ ฌ
        merge(array, helper, low, middle, high); // ๋ณ‘ํ•ฉ
    }
}

void merge(int[] array, int[] helper, int low, int middle, int high) {
    /* ์ ˆ๋ฐ˜์งœ๋ฆฌ ๋‘ ๋ฐฐ์—ด์„ helper ๋ฐฐ์—ด์— ๋ณต์‚ฌํ•œ๋‹ค. */
    for (int i = low; i <= high; i++) {
        helper[i] = array[i].
    }

    int helperLeft = low;
    int helperRight = middle + 1; 
    int current = low;

    /* helper ๋ฐฐ์—ด ์ˆœํšŒ. ์™ผ์ชฝ ์ ˆ๋ฐ˜๊ณผ ์˜ค๋ฅธ์ชฝ ์ ˆ๋ฐ˜์„ ๋น„๊ตํ•˜์—ฌ ์ž‘์€ ์›์†Œ๋ฅผ
        * ์›๋ž˜ ๋ฐฐ์—ด์— ๋ณต์‚ฌํ•ด ๋„ฃ๋Š”๋‹ค. */
    while (helperLeft <= middle && helperRight <= high) {
        if (helper[helperLeft] <= helper[helperRight]) {
                array[current] = helper[helperLeft];
                helperLeft++;
        } else { // ์˜ค๋ฅธ์ชฝ ์›์†Œ๊ฐ€ ์™ผ์ชฝ ์›์†Œ๋ณด๋‹ค ์ž‘์œผ๋ฉด
                array[current] = helper[helperRight];
                helperRight++;
        }
        current++;
    }

    /* ์™ผ์ชฝ ์ ˆ๋ฐ˜ ๋ฐฐ์—ด์— ๋‚จ์€ ์›์†Œ๋“ค์„ ์›๋ž˜ ๋ฐฐ์—ด์— ๋ณต์‚ฌํ•ด ๋„ฃ๋Š”๋‹ค. */
    int remaining = middle - helperLeft;
    for (int i = 0; i <= remaining; i++) {
        array[current + i] = helper[helperLeft + i];
    }
}
This post is licensed under CC BY 4.0 by the author.