2024. 1. 20. 23:34ใCS - Roadmap.sh/3. Common Algorithms
CS - 3. Common Algorithms - 3.1. Sorting - 3.1.6 Merge Sort
๐กMerge sort is a divide and conquer algorithm. It divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one.
๋ณํฉ ์ ๋ ฌ์ ๋ถํ ๋ฐ ์ ๋ณต ์๊ณ ๋ฆฌ์ฆ์ ๋๋ค. ์ด ํจ์๋ ์ ๋ ฅ ๋ฐฐ์ด์ ๋ ๊ฐ์ ๋ฐ์ผ๋ก ๋๋๊ณ , ๋ ๊ฐ์ ๋ฐ์ ๋ํด ์ค์ค๋ก๋ฅผ ํธ์ถํ ๋ค์ ์ ๋ ฌ๋ ๋ ๊ฐ์ ๋ฐ์ ๋ณํฉํฉ๋๋ค. merge() ํจ์๋ ๋ ๊ฐ์ ๋ฐ์ชฝ์ ๋ณํฉํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. merge(arr, l, m, r)๋ ๋ฐฐ์ด[l..m]๊ณผ ๋ฐฐ์ด[m+1..r]์ด ์ ๋ ฌ๋์ด ์๋ค๊ณ ๊ฐ์ ํ๊ณ ์ ๋ ฌ๋ ๋ ๊ฐ์ ํ์ ๋ฐฐ์ด์ ํ๋๋ก ๋ณํฉํ๋ ํต์ฌ ํ๋ก์ธ์ค์ ๋๋ค.
์ ๋ ฌ Sorting ์ ๋ง์ง๋ง Merge sort ๋ณํฉ ์ ๋ ฌ์ ๋๋ค.
๋ณํฉ ์ ๋ ฌ์ ํต์ ๋ ฌ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ถํ ์ ๋ณต ๋ฐฉ์์ ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ์ ๋๋ค.
์ฃผ์ด์ง ๋ฌธ์ ๋ฅผ ์์ ๋ถ๋ถ์ผ๋ก ๋๋ ๋ค ๊ฐ๊ฐ์ ํด๊ฒฐํ๊ณ , ์ด๋ฅผ ๋ค์ ํฉ์ณ์ ์ ์ฒด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐฉ์์ ์ฌ์ฉํฉ๋๋ค.
๋ณํฉ ์ ๋ ฌ์ ๋์ ๊ณผ์ ์ ์ด๋ ์ต๋๋ค.
1. ์ฃผ์ด์ง ๋ฐฐ์ด์ ์ ๋ฐ์ฉ ๋๋๋๋ค.
์ด๋ฅผ ์ฌ๊ท์ ์ผ๋ก ๋ฐ๋ณตํ์ฌ ๋ฐฐ์ด์ ๊ธธ์ด๊ฐ 1์ด ๋ ๋ ๊น์ง ๋๋๋๋ค.
๊ธธ์ด๊ฐ 1์ธ ๋ฐฐ์ด์ ์์ฐ์ค๋ฝ๊ฒ ์ ๋ ฌ๋ ์ํ๋ก ๊ฐ์ฃผํฉ๋๋ค.
2. ์ด๋ ๊ฒ ๋๋ ๋ฐฐ์ด๋ค์ ๋ค์ ํฉ์น๋ฉด์ ์ ๋ ฌํฉ๋๋ค.
์ด๋ฅผ ๋ณํฉ(Merge)๋ผ๊ณ ํ๋ฉฐ, ์ด ๊ณผ์ ์์ ์ค์ ์ ๋ ฌ์ด ์ด๋ฃจ์ด ์ง๋๋ค.
์ด๋ฅผ ์ฌ๊ท์ ์ผ๋ก ๋ฐ๋ณตํ์ฌ ์ ์ฒด ๋ฐฐ์ด์ด ์ ๋ ฌ๋ ๋๊น์ง ์ํํฉ๋๋ค.
In Python
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = arr[:mid]
right = arr[mid:]
return merge(merge_sort(left), merge_sort(right))
def merge(left, right):
result = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result
๋ณํฉ ์ ๋ ฌ์ ํญ์ O(n log n)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋๋ค.
์ด๋ ๊ฐ ๋จ๊ณ์์ n๊ฐ์ ์์๋ฅผ ๋ค๋ฃจ๊ณ , ์ ์ฒด ๋จ๊ณ์ ์๊ฐ log n์ด๊ธฐ ๋๋ฌธ์ ๋๋ค.
๋ณํฉ ์ ๋ ฌ์ ์์ ์ ์ธ ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ์ด๋ฏ๋ก ๋์ผํ ๊ฐ์ ๋ํด ์๋์ ์์๊ฐ ์ ์ง๋๋ ๊ฒ์ด ์ค์ํ ์ํฉ์์ ์ ์ฉํฉ๋๋ค.
๋ํ ๋ฐ์ดํฐ์ ํฌ๊ธฐ๊ฐ ํฌ๊ฑฐ๋, ์ธ๋ถ ์ ๋ ฌ์ด ํ์ํ ๊ฒฝ์ฐ์๋ ์ฌ์ฉ๋ฉ๋๋ค. ๊ทธ๋ฌ๋ ๋ณํฉ ์ ๋ ฌ์ ์ถ๊ฐ์ ์ธ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ํ์๋ก ํ๋๋ฐ, ์ด๋ ๋ณํฉ ๊ณผ์ ์์ ์๋ก์ด ๋ฐฐ์ด์ ์์ฑํ๊ธฐ ๋๋ฌธ์ ๋๋ค.
๋ฐ๋ผ์ ๋ฉ๋ชจ๋ฆฌ๊ฐ ์ ํ์ ์ธ ์ํฉ์์๋ ์ฌ์ฉํ๊ธฐ ์ด๋ ค์ธ ์ ์์ต๋๋ค.
์์์
'๋ณํฉ ์ ๋ ฌ์ ์์ ์ ์ธ ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ์ด๋ฏ๋ก ๋์ผํ ๊ฐ์ ๋ํด ์๋์ ์์๊ฐ ์ ์ง๋๋ ๊ฒ์ด ์ค์ํ ์ํฉ์์ ์ ์ฉํฉ๋๋ค.'
๋ผ๋ ๋ง์ ํ์ต๋๋ค.
๊ฐ๋จํ ์์๋ฅผ ๋ค์๋ฉด
items = [(2, 'B'), (1, 'B'), (2, 'A'), (1, 'A')]
์ด๋ฌํ ๋ฉ์ธํค(์ซ์int)์ ๋ณด์กฐํค(๋ฌธ์str)๋ก ๊ตฌ์ฑ๋์ด์๋ ํํ์ ๋ฆฌ์คํธ๊ฐ ์์ต๋๋ค.
๋ณํฉ์ ๋ ฌ์ ์์ ์ ์ธ ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ ์ด๋ฏ๋ก ์๋์ ์์๊ฐ ์ ์ง๋์ด์ผ ํฉ๋๋ค. ๋ฐ๋ผ์,
๋ญ์ฌ??? [(1, 'B'), (1, 'A'), (2, 'B'), (2, 'A')] ์ด๊ฒ ๋์์ผํ๋๋ฐ ์น ์ ๋ฆฌ๋์ ๋์๋ฒ๋ ธ๋ค.
(์ข๋ ๊ณต๋ถํ๋ค ์ฐจํ ์ถ๊ฐ)
๋ณํฉ ์ ๋ ฌ์์์ '์์ ์ฑ'์ด๋, ๋์ผํ ๊ฐ์ ๊ฐ์ง ์์๋ค ์ฌ์ด์ ์๋์ ์์๊ฐ ์ ๋ ฌ ํ์๋ ์ ์ง๋๋ค๋ ๊ฒ ์ ๋๋ค.
Appendix
LinkedList ์์์ Merge Sorting
๋ณํฉ ์ ๋ ฌ์ ์์ฐจ์ ์ธ ๋น๊ต๋ฅผ ํตํด ์ ๋ ฌํ๊ธฐ ๋๋ฌธ์, ์์ฐจ ์ ๊ทผ์ ์ฉ์ดํ LinkedList ์ ์ ๋ ฌ์ด ํ์ํ ๋ ๋งค์ฐ ํจ์จ์ ์ผ๋ก ๋์ํ๊ฒ ๋๋ค. ํต ์ํธ๋ ์์ฐจ ์ ๊ทผ์ด ์๋ ์์ ์ ๊ทผ์ ํตํด ์ ๋ ฌ์ ํ๊ธฐ ๋๋ฌธ์ ์ค๋ฒํค๋๊ฐ ๋ฐ์ํ์ฌ ์ฑ๋ฅ์ด ์ข์ง ์๋ค.
(https://velog.io/@haero_kim/%EC%95%88%EC%A0%95%EC%A0%81-%EA%B7%B8-%EC%9E%90%EC%B2%B4-Merge-Sort)
https://www.geeksforgeeks.org/merge-sort/
Merge Sort - Data Structure and Algorithms Tutorials - GeeksforGeeks
Like QuickSort , Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two
www.geeksforgeeks.org
'CS - Roadmap.sh > 3. Common Algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
3.2. Recursion - 3.2.2 Non-Tail Recursion (0) | 2024.01.22 |
---|---|
3.2. Recursion - 3.2.1 Tail Recursion (0) | 2024.01.21 |
3.1 Sorting - 3.1.5 Quick Sort (0) | 2024.01.16 |
3.1 Sorting - 3.1.4 Heap Sort (0) | 2024.01.15 |
3.1. Sorting - 3.1.3 Insertion Sort (0) | 2024.01.15 |