๏ฃฟ Apple Lover Developer & Artist

์˜์†์ ์ธ ๋””์ž์ธ์— ํ˜„๋Œ€์˜ ๊ณต๊ฐ์„ ์ฑ„์›Œ๋„ฃ๋Š” ๊ณต๋ฐฉ์ž…๋‹ˆ๋‹ค

๐Ÿ–ฅ Computer Science/Programming

[C++] ์ •๋ ฌ (Sorting)

singularis7 2019. 10. 19. 14:54
๋ฐ˜์‘ํ˜•

๋Š๋ฆฌ๋ฉด O(N^2), ๋น ๋ฅด๋ฉด O(NlogN) ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•ด๋‘์—ˆ๋‹ค. ์ฃผ๊ธฐ์ ์œผ๋กœ ๋“ค์–ด์™€์„œ ๋ณต์Šตํ•˜์ž!


๋ชฉํ‘œ

  • ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๋น„๊ตํ•ด๋ณด๋ฉฐ ๊ฐ์ข… ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํšจ์œจ์„ฑ ๋ฐ ํŠน์ง•์„ ํ•™์Šตํ•œ๋‹ค.

Selection Sort

  • ์„ ํƒ์ •๋ ฌ์€ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜๋ฅผ ์„ ํƒํ•˜์—ฌ ๊ฐ€์žฅ ๋’ท์ชฝ์œผ๋กœ ๋ณด๋‚ธ๋‹ค.
  • O(N^2) ์„ ๋งŒ์กฑํ•˜๋Š” ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘์—์„œ๋Š” ๋Š๋ฆฐ ์ถ•์— ์†ํ•œ๋‹ค.

Code

void selection_sort(vi &array) {
    for (int i=0; i<array.size(); i++) {
        int min_index = i;
        for (int j=i+1; j<array.size(); j++) {
            if (array[min_index] > array[j]) {
                min_index = j;
            }
        }
        swap(array[i], array[min_index]);
    }
}

int main()
{
    vi array = {1, 10, 5, 8, 7, 6, 4, 3, 2, 9};
    selection_sort(array);  // O(N^2), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    return 0;
}

Bubble Sort

  • ์ธ์ ‘ํ•œ ๊ฐ’๊ณผ ๋น„๊ตํ•˜์—ฌ ๋” ์ž‘์€ ๊ฐ’์„ ์•ž์œผ๋กœ ๋ณด๋‚ธ๋‹ค.
  • ์‹œ๊ฐ„๋ณต์žก๋„ O(N^2) ์„ ๋”ฐ๋ฅธ๋‹ค.
  • ์„ ํƒ ์ •๋ ฌ๊ณผ ๋น„๊ตํ•ด๋ณด์•˜์„ ๋•Œ swap ์—ฐ์‚ฐ์„ ๋” ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ์— ์ƒ๋Œ€์ ์œผ๋กœ ๋” ๋น„ํšจ์œจ์ ์ด๋‹ค.
  • ํ• ๋‹น ์—ฐ์‚ฐ์€ ๋ฉ”๋ชจ๋ฆฌ -> ๋ ˆ์ง€์Šคํ„ฐ, ๋ˆ„์ , ๋ ˆ์ง€์Šคํ„ฐ -> ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•ฉ์ณ์„œ ๊ตฌํ˜„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—ฐ์‚ฐ๋Ÿ‰์ด ๋” ๋งŽ๋‹ค.

Code

void burble_sort(vi &array) {
    for (int i=0; i<array.size(); i++) {
        for (int j=0; j<array.size()-1-i; j++) {
            if (array[j] > array[j+1]) {
                swap(array[j], array[j+1]);
            }
        }
    }
}

int main()
{
    vi array = {1, 10, 5, 8, 7, 6, 4, 3, 2, 9};
    burble_sort(array);     // O(N^2), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    return 0;
}

Insertion Sort

  • ์ •๋ ฌ๋œ ๋ถ€๋ถ„๊ณผ ์ •๋ ฌํ•ด์•ผํ•  ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆŒ ๋•Œ ์ •๋ ฌํ•  ์นด๋“œ๋ฅผ ์ •๋ ฌ๋œ ๋ถ€๋ถ„์— ์ ์ • ์œ„์น˜์— ์‚ฝ์ž…ํ•œ๋‹ค.
  • ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฑฐ์˜ ์ •๋ ฌ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ ๊ฐ€์žฅ ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ด๋‹ค.

Code

void insertion_sort(vi &array) {
    for (int i=0; i<array.size(); i++) {
        for (int j=i; j>0; j--) {
            if (array[j-1] > array[j]) {
                swap(array[j], array[j-1]);
            }
        }
    }
}

int main()
{
    vi array = {1, 10, 5, 8, 7, 6, 4, 3, 2, 9};
    insertion_sort(array);  // O(N^2), [1, 2, 3, 4, 5, 6, 7, 8, 9]
    return 0;
}

Quick Sort

  • ํŠน์ • ๊ธฐ์ค€๊ฐ’์œผ๋กœ ํฐ์ˆ˜์™€ ์ž‘์€์ˆ˜๋ฅผ ๋‚˜๋ˆˆ ๋’ค์— ์ •๋ ฌ ํ•œ๋‹ค.
  • ํ€ต์ •๋ ฌ์˜ ๊ตฌํ˜„์—๋Š” ๋ถ„ํ• ์ •๋ณต ๊ธฐ๋ฒ•์ด ์ˆจ์–ด์žˆ๋‹ค.
  • ํ‰๊ท  O(NlogN) ์„ ๋งŒ์กฑํ•˜๋ฉฐ ์ตœ์•…์˜ ๊ฒฝ์šฐ O(N^2) ์„ ๋งŒ์กฑํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ตœ์•…์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฑฐ์˜ ์ •๋ ฌ๋˜์–ด ์žˆ์„ ๊ฒฝ์šฐ์ด๋‹ค.
  • ๋ฐ์ดํ„ฐ ํŠน์„ฑ์— ์•Œ๋งž๊ฒŒ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ ํƒ ํ•„์š”ํ•จ.

Figure: Quick sort Implement

Code

void quick_sort(vi &array, int start, int end) {
    if (start >= end) return;

    int pivot = start;
    int i = pivot+1; int j = end;

    while (i <= j) {
        while (i <= end && array[pivot] >= array[i]) i++;
        while (j > start && array[pivot] <= array[j]) j--;
        int idx = (i > j)? pivot:i;
        swap(array[idx], array[j]);
    }

    quick_sort(array, start, j-1);
    quick_sort(array, j+1, end);
}

void quick_sort(vi array) {
    quick_sort(array, 0, array.size()-1);
    to_string(array);
}

int main()
{
    vi array = {1, 10, 5, 8, 7, 6, 4, 3, 2, 9};
    quick_sort(array);      // AVG: O(NlogN) Worst Case: O(N^2) (sorted array) 
    return 0;
}

Merge Sort

  • ์ž‘์€ ๋‹จ์œ„๋กœ ๋ถ„ํ• ํ•˜์—ฌ ๋“ค์–ด๊ฐ”๋‹ค๊ฐ€ ์ •๋ ฌํ•˜๋ฉด์„œ ๋‚˜์˜จ๋‹ค.
  • ๋ณ‘ํ•ฉ ์ •๋ ฌ์˜ ๊ตฌํ˜„์—๋Š” ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•œ ๋ถ„ํ•  ์ •๋ณต ๊ธฐ๋ฒ•์ด ์ˆจ์–ด์žˆ๋‹ค.
  • ์ „์—ญ๋ณ€์ˆ˜๋กœ ์„ค์ •๋œ ์˜ˆ๋น„ sorted ๋ฐฐ์—ด์ด ํ•„์š”ํ•˜๋‹ค.
  • ํ‰๊ท  O(NlogN) ์„ ๋งŒ์กฑํ•˜์ง€๋งŒ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ๋น„ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

Code

void merge(vi &array, vi &sorted, int start, int mid, int end) {
    int i = start; int j = mid+1; int k = start;
    while (i<=mid && j<=end) {
        sorted[k++] = (array[i] <= array[j])? array[i++]:array[j++];
    }
    
    int index = (i>mid)? j:i;
    for (int m=index; m<=end; m++) {
        sorted[k++] = array[m];
    }
    for (int m=start; m<=end; m++) {
        array[m] = sorted[m];
    }
}

void merge_sort(vi &array, vi &sorted, int start, int end) {
    if (start >= end) return;
    
    int mid = (start + end) / 2;
    merge_sort(array, sorted, start, mid);
    merge_sort(array, sorted, mid+1, end);
    merge(array, sorted, start, mid, end);
}

void merge_sort(vi array) {
    vi sorted(array.size(), 0);
    merge_sort(array, sorted, 0, array.size()-1);
}

int main()
{
    vi array = {1, 10, 5, 8, 7, 6, 4, 3, 2, 9};
    merge_sort(array);      // AVG: O(NlogN) Worst Case: Time Complexity O(NlogN)
    return 0;		    // But large memory use
}

Heap Sort

  • Binary Tree (์ด์ง„ ํŠธ๋ฆฌ): ๋ชจ๋“  ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ๊ฐ€ 2๊ฐœ ์ดํ•˜์ธ ๊ตฌ์กฐ
  • Complete Binary Tree (์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ): ๋ฐ์ดํ„ฐ๊ฐ€ Root ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์ž์‹๋…ธ๋“œ์— ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ ์ˆœ์„œ๋Œ€๋กœ ์ฑ„์›Œ์ง€๋Š” ์ด์ง„ํŠธ๋ฆฌ๊ตฌ์กฐ

  • Heap: ์ตœ์†Œ๊ฐ’์ด๋‚˜ ์ตœ๋Œ“๊ฐ’์„ ๋น ๋ฅด๊ฒŒ ์ฐพ์•„๋‚ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ์™„์ „์ด์ง„ํŠธ๋ฆฌ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ํŠธ๋ฆฌ๊ตฌ์กฐ
    1. Max Heap: ๋ถ€๋ชจ๋…ธ๋“œ > ์ž์‹๋…ธ๋“œ ๊ด€๊ณ„๋ฅผ ๋งŒ์กฑํ•œ๋‹ค
    2. Min Heap: ๋ถ€๋ชจ๋…ธ๋“œ < ์ž์‹๋…ธ๋“œ ๊ด€๊ณ„๋ฅผ ๋งŒ์กฑํ•œ๋‹ค
  • Heapify ํž™ ์ƒ์„ฑ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•˜์—ฌ Heap Sort ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

Code

void heap_sort(vi array) {
    const int SIZE = array.size();
    for (int i=1; i<SIZE; i++) {
        int ptr = i;
        while (ptr != 0) {
            int root = (ptr-1) / 2;
            if (array[root] < array[ptr]) {
                swap(array[ptr], array[root]);
            }
            ptr = root;
        }
    }

    for (int i=SIZE-1; i>=0; i--) {
        swap(array[0], array[i]);
        int root = 0; int c = 1;
        while (c < i) {
            c = 2 * root + 1;
            if (c < i - 1 && array[c] < array[c+1]) {
                c++;
            }
            if (c < i && array[root] < array[c]) {
                swap(array[root], array[c]);
            }
            root = c;
        }
    }
}

int main()
{
    vi array = {1, 10, 5, 8, 7, 6, 4, 3, 2, 9};
    heap_sort(array);       // AVG: O(NlogN)
    return 0;
}

Counting Sort

  • ์ž‘์€ ๋ฒ”์œ„์— ๋Œ€ํ•ด์„œ ๊ฐ’์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์–ด ์ˆœ์ฐจ์ ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค
  • ๋ฒ”์œ„ ๋งŒํผ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ ๋’ค์— ๋ฐฐ์—ด๊ฐ’์ด ์กด์žฌํ•˜๋Š” ๋งŒํผ ํ•ด๋‹น ์ธ๋ฑ์Šค๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.
  • ์‹œ๊ฐ„๋ณต์žก๋„ O(N) ์„ ๋งŒ์กฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— O(NlogN) ์ด ์•ˆ๋จนํžˆ๋ฉด ํ•œ๋ฒˆ์ฏค ์ƒ๊ฐํ•ด๋ณผ๋งŒํ•˜๋‹ค.
๋ฐ˜์‘ํ˜•