๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“š Algorithm/Algorithm Note

์•Œ๊ณ ๋ฆฌ์ฆ˜ - Sort ์ •๋ ฌ (Merge sort, Quick sort), Python ์ฝ”๋“œ

Merge Sort (๋ณ‘ํ•ฉ์ •๋ ฌ)

- ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„ํ–ˆ์„ ๋•Œ ์ด ์ •๋ ฌ์€ ์•ˆ์ •์ •๋ ฌ์— ์†ํ•˜๋ฉฐ, ๋ถ„ํ•  ์ •๋ณต ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘์˜ ํ•˜๋‚˜์ด๋‹ค.

- ๋ถ„ํ•  ์ •๋ณต ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ๋ฌธ์ œ๋ฅผ ์ž‘์€ 2๊ฐœ์˜ ๋ฌธ์ œ๋กœ ๋ถ„๋ฆฌํ•˜๊ณ  ๊ฐ๊ฐ์„ ํ•ด๊ฒฐํ•œ ๋‹ค์Œ, ๊ฒฐ๊ณผ๋ฅผ ๋ชจ์•„์„œ ์›๋ž˜์˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์ „๋žต์ด๋‹ค. ๋Œ€๊ฐœ ์ˆœํ™˜ํ˜ธ์ถœ์„ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•œ๋‹ค. 

- ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‘ ๊ฐœ์˜ ๊ท ๋“ฑํ•œ ํฌ๊ธฐ๋กœ ๋ถ„ํ• ํ•˜๊ณ  ๋ถ„ํ• ๋œ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•œ ๋‹ค์Œ, ๋‘ ๊ฐœ์˜ ์ •๋ ฌ๋œ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•ฉํ•˜์—ฌ ์ „์ฒด๊ฐ€ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋˜๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

 

<๋ณ‘ํ•ฉ ์ •๋ ฌ์˜ ๋‹จ๊ณ„>
- ๋ถ„ํ• (Divide): ์ž…๋ ฅ ๋ฐฐ์—ด์„ ๊ฐ™์€ ํฌ๊ธฐ์˜ 2๊ฐœ์˜ ๋ถ€๋ถ„ ๋ฐฐ์—ด๋กœ ๋ถ„ํ• ํ•œ๋‹ค.
- ์ •๋ณต(Conquer): ๋ถ€๋ถ„ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•œ๋‹ค. ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ์ถฉ๋ถ„ํžˆ ์ž‘์ง€ ์•Š์œผ๋ฉด ์ˆœํ™˜ ํ˜ธ์ถœ ์„ ์ด์šฉํ•˜์—ฌ ๋‹ค์‹œ ๋ถ„ํ•  ์ •๋ณต ๋ฐฉ๋ฒ•์„ ์ ์šฉํ•œ๋‹ค.
- ๊ฒฐํ•ฉ(Combine): ์ •๋ ฌ๋œ ๋ถ€๋ถ„ ๋ฐฐ์—ด๋“ค์„ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด์— ๋ณ‘ํ•ฉํ•œ๋‹ค.

 

Merge sort ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํŠน์ง•

์žฅ์  : ํฌ๊ธฐ๊ฐ€ ํฐ ๋ ˆ์ฝ”๋“œ๋ฅผ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •๋ ฌํ•  ๊ฒฝ์šฐ ํšจ์œจ์ ์ž„, ๋ฐ์ดํ„ฐ ๋ถ„ํฌ์— ์˜ํ–ฅ์„ ๋œ ๋ฐ›๋Š”๋‹ค. 

๋‹จ์  : ํฌ๊ธฐ๊ฐ€ ํฐ ๋ ˆ์ฝ”๋“œ๋“ค์„ ๊ทธ๋ƒฅ ์ •๋ ฌํ•  ๊ฒฝ์šฐ ์‹œ๊ฐ„ ๋‚ญ๋น„๊ฐ€ ์‹ฌํ•จ (์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ํ™œ์šฉํ•˜์—ฌ ํ•ด๊ฒฐ)

 

 

Python์œผ๋กœ ๋‚˜ํƒ€๋‚ธ Merge sort

#best : nlogn
#avg : nlogn
#worst : nlogn
def mergeSort(arr) :
    if len(arr) > 1 :
        mid = len(arr) // 2
        L = arr[:mid]
        R = arr[mid:] 
        mergeSort(L)
        mergeSort(R)    
        i, j, k = 0, 0, 0 
        while i < len(L) and j < len(R) :
            if L[i] < R[j] :
                arr[k] = L[i]
                i += 1
            else :
                arr[k] = R[j]
                j += 1
            k += 1
            
        while i < len(L) :
            arr[k] = L[i]
            i += 1
            k += 1
        while j < len(R) :
            arr[k] = R[j]
            j += 1
            k += 1
        return arr

ll = [1, 2, 3, 2, 5, 6]
print(mergeSort(ll))

 

 

 

Quick sort (ํ€ต์ •๋ ฌ)

- ํ€ต ์ •๋ ฌ์€ ๋ถˆ์•ˆ์ • ์ •๋ ฌ ์— ์†ํ•˜๋ฉฐ, ๋‹ค๋ฅธ ์›์†Œ์™€์˜ ๋น„๊ต๋งŒ์œผ๋กœ ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋น„๊ต ์ •๋ ฌ ์— ์†ํ•œ๋‹ค.
- ๋ถ„ํ•  ์ •๋ณต ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ๋ณ‘ํ•ฉ ์ •๋ ฌ๊ณผ ๋‹ฌ๋ฆฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋น„๊ท ๋“ฑํ•˜๊ฒŒ ๋ถ„ํ• ํ•œ๋‹ค. 

- ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ”ผ๋ฒ—(pivot)์„ ๊ธฐ์ค€์œผ๋กœ ๋‘ ๊ฐœ์˜ ๋น„๊ท ๋“ฑํ•œ ํฌ๊ธฐ๋กœ ๋ถ„ํ• ํ•˜๊ณ  ๋ถ„ํ• ๋œ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•œ ๋‹ค์Œ, ๋‘ ๊ฐœ์˜ ์ •๋ ฌ๋œ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•ฉํ•˜์—ฌ ์ „์ฒด๊ฐ€ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋˜๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

- ์ˆœํ™˜ ํ˜ธ์ถœ์ด ํ•œ๋ฒˆ ์ง„ํ–‰๋  ๋•Œ๋งˆ๋‹ค ์ตœ์†Œํ•œ ํ•˜๋‚˜์˜ ์›์†Œ(ํ”ผ๋ฒ—)๋Š” ์ตœ์ข…์ ์œผ๋กœ ์œ„์น˜๊ฐ€ ์ •ํ•ด์ง€๋ฏ€๋กœ, ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋ฐ˜๋“œ์‹œ ๋๋‚œ๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ๋‹ค

 

 

<ํ€ต ์ •๋ ฌ์˜ ๋‹จ๊ณ„>
- ๋ถ„ํ• (Divide): ์ž…๋ ฅ ๋ฐฐ์—ด์„ ํ”ผ๋ฒ—์„ ๊ธฐ์ค€์œผ๋กœ ๋น„๊ท ๋“ฑํ•˜๊ฒŒ 2๊ฐœ์˜ ๋ถ€๋ถ„ ๋ฐฐ์—ด(ํ”ผ๋ฒ—์„ ์ค‘์‹ฌ์œผ๋กœ ์™ผ์ชฝ: ํ”ผ๋ฒ—๋ณด๋‹ค ์ž‘์€ ์š”์†Œ๋“ค, ์˜ค๋ฅธ์ชฝ: ํ”ผ๋ฒ—๋ณด๋‹ค ํฐ ์š”์†Œ๋“ค)๋กœ ๋ถ„ํ• ํ•œ๋‹ค.
- ์ •๋ณต(Conquer): ๋ถ€๋ถ„ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•œ๋‹ค. ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ์ถฉ๋ถ„ํžˆ ์ž‘์ง€ ์•Š์œผ๋ฉด ์ˆœํ™˜ ํ˜ธ์ถœ ์„ ์ด์šฉํ•˜์—ฌ ๋‹ค์‹œ ๋ถ„ํ•  ์ •๋ณต ๋ฐฉ๋ฒ•์„ ์ ์šฉํ•œ๋‹ค.
- ๊ฒฐํ•ฉ(Combine): ์ •๋ ฌ๋œ ๋ถ€๋ถ„ ๋ฐฐ์—ด๋“ค์„ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด์— ํ•ฉ๋ณ‘ํ•œ๋‹ค.

 

 

Quick sort ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํŠน์ง•

์žฅ์  : ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค. (๊ฐ€์žฅ ๋น ๋ฆ„)

๋‹จ์  : ์ด๋ฏธ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด์„œ๋Š” ํ€ต ์ •๋ ฌ์˜ ๋ถˆ๊ท ํ˜• ๋ถ„ํ• ์— ์˜ํ•ด ์˜คํžˆ๋ ค ์ˆ˜ํ–‰์‹œ๊ฐ„์ด ๋” ๋งŽ์ด ๊ฑธ๋ฆฐ๋‹ค.

 

Python์œผ๋กœ ๋‚˜ํƒ€๋‚ธ Quick sort

#quick sort
def partition(arr, low, high):
    i = (low - 1)
    pivot = arr[high]

    for j in range(low, high):
        if arr[j] <= pivot:
            i += 1
            arr[i], arr[j] = arr[j], arr[i]
    arr[i+1], arr[high] = arr[high], arr[i+1]
    return (i+1)

def quickSort(arr, low, high):
    if len(arr) == 1:
        return arr
    if low < high:
        pi = partition(arr, low, high)
        quickSort(arr, low, pi-1)
        quickSort(arr, pi+1, high)
        
        
        
def Quick(arr) :
    if len(arr) <= 1:
        return arr
    pivot = arr[len(arr) // 2]
    less, equal, more = [], [], []
    for num in arr:
        if num < pivot:
            less.append(num)
        elif num > pivot:
            more.append(num)
        else:
            equal.append(num)
    return Quick(less) + equal + Quick(more)

 

 

 

 

 

 

[์„ ํƒ, ์‚ฝ์ž…, ๋ฒ„๋ธ” ์ •๋ ฌ] 

2023.04.05 - [์•Œ๊ณ ๋ฆฌ์ฆ˜] - ์•Œ๊ณ ๋ฆฌ์ฆ˜ - Sort ์ •๋ ฌ (Selection sort, Insertion sort, Bubble sort), Python ์ฝ”๋“œ

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ - Sort ์ •๋ ฌ (Selection sort, Insertion sort, Bubble sort), Python ์ฝ”๋“œ

Sort (์ •๋ ฌ) n๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด๋ฅผ ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์ •ํ•œ ๊ธฐ์ค€์— ๋งž๊ฒŒ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ Sort ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ข…๋ฅ˜ Selection sort Insertion sort Bubble sort Merge sort Quick sort ๊ธฐ์ดˆ์ ์ธ ์ •

mcrkgus.tistory.com

 

728x90