๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐˜š๐˜ญ๐˜ฐ๐˜ธ ๐˜ฃ๐˜ถ๐˜ต ๐˜ด๐˜ต๐˜ฆ๐˜ข๐˜ฅ๐˜บ

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ์ •๋ ฌ - 1. ๋ณ‘ํ•ฉ ์ •๋ ฌ(Merge Sort) ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ์ •๋ ฌ - 1. ๋ณ‘ํ•ฉ ์ •๋ ฌ(Merge Sort)

.23 2021. 7. 14. 21:18

1. ๋ณ‘ํ•ฉ ์ •๋ ฌ ; Merge Sort

1.1 ๊ฐœ๋…

๋ถ„ํ•  ์ •๋ณต ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•œ ๋Œ€ํ‘œ์ ์ธ ์ •๋ ฌ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋กœ, ์—ฌ๋Ÿฌ ์ •๋ ฌ๋œ ์ง‘ํ•ฉ๋“ค์„ ๋ณ‘ํ•ฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ์ •๋ ฌ๋œ ์ง‘ํ•ฉ์œผ๋กœ ๋งŒ๋“œ๋Š” ์ •๋ ฌ๋ฐฉ๋ฒ•์„ ์˜๋ฏธํ•œ๋‹ค. ๋ณ‘ํ•ฉ ์ •๋ ฌ์€ ๋‹ค์Œ ์„ธ ๊ฐ€์ง€์˜ ๋‹จ๊ณ„๋ฅผ ํ†ตํ•ด ์ง„ํ–‰๋œ๋‹ค.

 

1. ๋ถ„ํ• (Divide) : ํฐ ์ง‘ํ•ฉ์„ ๋ถ€๋ถ„์ง‘ํ•ฉ์œผ๋กœ ๋ถ„ํ• 

2. ์ •๋ณต(Conquer) : ๋ถ€๋ถ„์ง‘ํ•ฉ ๋‚ด์˜ ์›์†Œ ์ •๋ ฌ

3. ๊ฒฐํ•ฉ(Combine) : ์ •๋ ฌ๋œ ๋ถ€๋ถ„์ง‘ํ•ฉ๋“ค์„ ํ•˜๋‚˜์˜ ์ง‘ํ•ฉ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ๊ฒฐํ•ฉ

 

์›๋ฆฌ๋งŒ ์ƒ๊ฐํ•˜๋ฉด ๋ณต์žกํ•ด ๋ณด์ด์ง€๋งŒ, ๊ทธ๋ฆผ๊ณผ ํ•จ๊ป˜ ๋ณด๋ฉด ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค.

์ •๋ ฌ๋˜์ง€ ์•Š์€ ์ง‘ํ•ฉ A๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค.

A = { 3, 8, 7, 2, 5, 1, 4, 6 }

์ง‘ํ•ฉ A๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •์„ ํ•ด๋ณด์ž. ๋ณ‘ํ•ฉ ์ •๋ ฌํ•˜๋Š” ๊ณผ์ •์€ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™๋‹ค.

 

๋ณ‘ํ•ฉ ์ •๋ ฌ ๊ณผ์ •

์ง‘ํ•ฉ A๋ฅผ ๋” ์ด์ƒ ๋ถ„ํ•ด๋˜์ง€ ์•Š๋Š” ๊ฐ€์žฅ ์ž‘์€ ํฌ๊ธฐ์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์œผ๋กœ ๋ถ„ํ• ํ•œ ๋‹ค์Œ(1~4๋‹จ๊ณ„), ๋ฐฐ์—ด์„ ํ•ฉ์น˜๋Š”(๊ฒฐํ•ฉ) ๊ณผ์ •์—์„œ ์˜ค๋ฆ„์ฐจ์ˆœ์„ ์œ ์ง€ํ•˜๋ฉฐ ์ •๋ ฌํ•œ๋‹ค(5~7๋‹จ๊ณ„). ์ด ๋•Œ ์ง‘ํ•ฉ ๋ช‡ ๊ฐœ๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ์ง‘ํ•ฉ์œผ๋กœ ๋งŒ๋“ค ๊ฒƒ์ธ์ง€์— ๋”ฐ๋ผ n-way ๋ณ‘ํ•ฉ์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š”๋ฐ, ์œ„์˜ ์˜ˆ์‹œ๋กœ ๋“  ๋ณ‘ํ•ฉ ๋ฐฉ๋ฒ•์€ ๋‘ ๊ฐœ์˜ ์ง‘ํ•ฉ์„ ๊ฒฐํ•ฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ์ง‘ํ•ฉ์œผ๋กœ ๋งŒ๋“ค์—ˆ๊ธฐ ๋•Œ๋ฌธ์— 2-way ๋ณ‘ํ•ฉ ์ •๋ ฌ์ด๋‹ค.

 

1.2 ๊ตฌํ˜„

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

 

#1. merge(int* sorted, int* arr, int begin, int mid, int end)

void merge(int* sorted, int* arr, int begin, int mid, int end) {
    int i, j, k;
    i = begin;		// ์ฒซ ๋ฒˆ์งธ ๋ถ€๋ถ„์ง‘ํ•ฉ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ
    j = mid + 1;	// ๋‘ ๋ฒˆ์งธ ๋ถ€๋ถ„์ง‘ํ•ฉ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ
    k = begin;		// ๋ณ‘ํ•ฉํ•  ์ง‘ํ•ฉ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ

    while(i <= mid && j <= end) {
        if(arr[i] <= arr[j]) {
            sorted[k] = arr[i];
            i++;
        }
        else {
            sorted[k] = arr[j];
            j++;
        }
        k++;
    }
    if(i > mid) {
        for(int a = j; a <= end; a++, k++)
            sorted[k] = arr[a];
    }
    else {
        for(int a = i; a <= mid; a++, k++)
            sorted[k] = arr[a];
    }

    for(int a = begin; a <= end; a++)
        arr[a] = sorted[a];
}

 

#2. mergeSort(int* sorted, int* arr, int begin, int end)

void mergeSort(int* sorted, int* arr, int begin, int end) {
    int mid;
    if(begin < end) {
        mid = (begin + end) / 2;
        mergeSort(sorted, arr, begin, mid);	// ์™ผ์ชฝ ๋ถ€๋ถ„์ง‘ํ•ฉ ๋ถ„ํ• 
        mergeSort(sorted, arr, mid + 1, end);	// ์˜ค๋ฅธ์ชฝ ๋ถ€๋ถ„์ง‘ํ•ฉ ๋ถ„ํ• 
        merge(sorted, arr, begin, mid, end);	// ๋ถ€๋ถ„์ง‘ํ•ฉ ๋ณ‘ํ•ฉ
    }
}

 

Comments