DevLog ๐Ÿ“จ/Algorithm

[DevLog] [Alogorithm] ์ •๋ ฌ Sorting Problem

cece00 2023. 8. 13. 22:58


๐Ÿ‘† Sound of Sorting

์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๋ถ„๋ฅ˜

  • Stable Sorting : ์ค‘๋ณต๋œ ์›์†Œ์˜ ์ˆœ์„œ๊ฐ€ ์ •๋ ฌ๋œ ํ›„์—๋„ ์œ ์ง€๋จ์ด ๋ณด์žฅ๋œ๋‹ค.
  • Unstable Sorting : ์ค‘๋ณต๋œ ์›์†Œ์˜ ์ˆœ์„œ๊ฐ€ ์ •๋ ฌ๋œ ํ›„์— ์œ ์ง€๋จ์ด ๋ณด์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค.
    K1, .., K2, .. ์ˆœ์„œ์ธ ๋ฐฐ์—ด์—์„œ ์ •๋ ฌ ํ›„ K1, K2 ์ž„์ด ๋ณด์žฅ๋˜๋ฉด stable, K2, K1์ผ ์ˆ˜ ์žˆ์œผ๋ฉด unstable
  • in-place: ๋‚ด๋ถ€ ์ •๋ ฌ, ์ •๋ ฌ์„ ์œ„ํ•ด ์™ธ๋ถ€ ๋ฐฐ์—ด์ด ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค.
  • not in-place: ์™ธ๋ถ€ ์ •๋ ฌ, ์ •๋ ฌ์„ ์œ„ํ•ด ์™ธ๋ถ€ ๋ฐฐ์—ด์ด ํ•„์š”ํ•˜๋‹ค.

๐Ÿ”ฅ ํ•ต์‹ฌ์€ **๊ตํ™˜/์„ ํƒ/์‚ฝ์ž…** ์ด๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ด ์„ธ ๊ฐ€์ง€ ๊ฐœ๋…์„ ์‘์šฉํ•œ๋‹ค.

1. O(N^2) ์•Œ๊ณ ๋ฆฌ์ฆ˜

1. ๊ตํ™˜ ์ •๋ ฌ Exchange Sort

  for i in range(0, N-1):
    for j in range(i+1, N):
      if A[i] < A[j]:
        swap(A[i], A[j])

2. ์‚ฝ์ž… ์ •๋ ฌ Insertion Sort

๋ชจ๋“  i = 0~N-1 ์— ๋Œ€ํ•ด j = i-1๋ถ€ํ„ฐ 0์œผ๋กœ ๋‚ด๋ ค๊ฐ€๋ฉด์„œ, ๋งŒ์•ฝ A[j] > A[i] ์ด๋ฉด ํ•œ ์นธ ์•ž์œผ๋กœ ๋ณต์‚ฌํ•œ๋‹ค. ๋” ์ด์ƒ A[i] ๋ณด๋‹ค ํฐ ๊ฐ’์ด ์—†์œผ๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•˜๊ณ , j+1 ํฌ์ธํ„ฐ ์ž๋ฆฌ์— A[i] ์„ ๋„ฃ๋Š”๋‹ค.

def insertion_sort():
  for i in range(1, N):
    next = A[i]
    for j in range(i-1, -1, -1):
      if A[j] > next:
        A[j+1] = A[j]
        break
    A[j+1] = next

3. ์„ ํƒ ์ •๋ ฌ Selection Sort

i = 0~N-2 ์— ๋Œ€ํ•ด, j = i+1~N-1 ์ค‘ ์ตœ๋Œ“๊ฐ’์„ ๊ณจ๋ผ A[i] ์™€ swap ํ•œ๋‹ค.

for i in range(0, N-1):
  m = i, min = A[m]
  for j in range(i+1, N):
    if A[j] < min:
      m = j
      min = A[j]
  swap(A[i], A[m])

4. ๋ฒ„๋ธ” ์ •๋ ฌ Bubble Sort

j = 0~N-1 ์— ๋Œ€ํ•ด A[j], A[j+1] ์„ ๋น„๊ตํ•˜์—ฌ ์ž‘์€ ๊ฐ’์ด ์•ž์— ์žˆ์œผ๋ฉด swap ํ•˜๋Š” ๊ฒƒ์„ N๋ฒˆ ๋ฐ˜๋ณตํ•œ๋‹ค.

for i in range(0, N):
  for j in range(0, N-1):
    if A[j] > A[j+1]:
      swap(A[j], A[j+1])

2. O(logN) ์•Œ๊ณ ๋ฆฌ์ฆ˜

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

1. ๋ฐฐ์—ด์„ ๋‘ ๋ถ€๋ถ„ L, R ๋กœ ๋‚˜๋ˆˆ๋‹ค.
2. ๊ฐ ๋ถ€๋ถ„์„ ์ •๋ ฌํ•œ๋‹ค.
3. ๋‘ ๋ฐฐ์—ด์„ ์ˆœ์„œ๋Œ€๋กœ mergeํ•œ๋‹ค.
  i, j, k = 0
    while i < j:
      L[i]๊ณผ R[j]์„ ๋น„๊ตํ•ด ์ž‘์€ ๊ฐ’์„ M[k]์— ๋†“๋Š”๋‹ค.
      ๋” ์ž‘์€ ์ชฝ์˜ ํฌ์ธํ„ฐ๋ฅผ 1 ์ฆ๊ฐ€ํ•œ๋‹ค.

    i > len(L) ์ธ ๊ฒฝ์šฐ
      R๋ฐฐ์—ด์˜ ๋‚จ์€ ์›์†Œ๋ฅผ M์— ๋†“๋Š”๋‹ค.
    else
      L๋ฐฐ์—ด์˜ ๋‚จ์€ ์›์†Œ๋ฅผ M์— ๋†“๋Š”๋‹ค.
  M์€ merge๋œ ๋ฐฐ์—ด์ด๋‹ค.

merge ๊ตฌํ˜„ ์‹œ ์กฐ์‹ฌํ•  ์ 

merged ๋ฐฐ์—ด์ธ M์„ ํ•จ์ˆ˜ ๋‚ด ๋กœ์ปฌ ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•˜๋ฉด์„œ, ์• ์ดˆ ์ธํ’‹ ๊ธธ์ด์ธ N๋งŒํผ์˜ ๋ฐฐ์—ด์„ ์„ ์–ธํ–ˆ๋˜ ๊ฒƒ์ด ์˜ค๋ฅ˜์˜€๋‹ค. ํ•ญ์ƒ mergeํ•  ๊ธธ์ด๋งŒํผ์˜ ๋ฐฐ์—ด๋งŒ ์„ ์–ธํ•˜์ž. (๐Ÿ‘€ ๊ด€๋ จ: ๋ณ‘ํ•ฉ ์ •๋ ฌ์˜ merge ํ•จ์ˆ˜๋Š” ๋‹ด๋‹น ๋ฒ”์œ„๋ณด๋‹ค ํฐ ์—ฐ์‚ฐ์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค.)

2. ํ€ต ์ •๋ ฌ Quich Sort

1. ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ฅผ pivot์œผ๋กœ ์ •ํ•œ๋‹ค.
2. pivot์„ ๊ธฐ์ค€์œผ๋กœ pivot๋ณด๋‹ค ์ž‘์€ ๊ฐ’์„ ์™ผ์ชฝ์œผ๋กœ, ํฐ ๊ฐ’์„ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ถ„๋ฆฌํ•œ๋‹ค.
3. ์™ผ์ชฝ ๋ถ€๋ถ„์„ ์ •๋ ฌํ•œ๋‹ค.
4. ์˜ค๋ฅธ์ชฝ ๋ถ€๋ถ„์„ ์ •๋ ฌํ•œ๋‹ค.

partition ํ•จ์ˆ˜๋Š” ๊ฐ„๋‹จํžˆ ํ•จ์ˆ˜ ๋‚ด ๋กœ์ปฌ ๋ฐฐ์—ด L, R์„ ์„ ์–ธํ•ด ์ •๋ ฌํ•œ ๋‹ค์Œ, L + pivot + R ๋กœ ๋‹ค์‹œ ํ•ฉ์ณ ์ฃผ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๊ณ , ๊ตํ™˜์„ ์ด์šฉํ•ด pivot ์ธ๋ฑ์Šค๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

partition ๊ณผ์ •

Is Quick Sort Stable? in-place๋Š” stable ํ•˜์ง€ ์•Š๊ณ  not in-place๋Š” stableํ•˜๋‹ค.

3. ํž™ ์ •๋ ฌ Heap Sort

What is heap?

๋ถ€๋ชจ์˜ ๊ฐ’์ด ์ž์‹์˜ ๊ฐ’๋ณด๋‹ค ํผ/์ž‘์Œ์ด ํ•ญ์ƒ ๋ณด์žฅ๋˜๋Š” ์ด์ง„ ํŠธ๋ฆฌ ํ˜•์‹์˜ ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค. ๋ถ€๋ชจ์˜ ๊ฐ’์ด ์ž์‹์˜ ๊ฐ’๋ณด๋‹ค ํ•ญ์ƒ ํด ๊ฒฝ์šฐ max heap(์ตœ๋Œ€ ํž™), ํ•ญ์ƒ ์ž‘์„ ๊ฒฝ์šฐ min heap(์ตœ์†Œ ํž™) ์ด๋ผ๊ณ  ํ•œ๋‹ค. max heap์˜ root๋Š” ํž™์˜ ์ตœ๋Œ“๊ฐ’์ด๊ณ , min heap์˜ root๋Š” ํž™์˜ ์ตœ์†Ÿ๊ฐ’์ด๋‹ค.

1. heapify: ๋ฐฐ์—ด์„ ์ตœ๋Œ€ ํž™์œผ๋กœ ๋งŒ๋“ ๋‹ค.
  parent = 0~N//2๊นŒ์ง€์˜ ๋…ธ๋“œ์— ๋Œ€ํ•ด
    while i > N//2
      if ์ž์‹ ์ค‘ ํฐ ๊ฐ’๋ณด๋‹ค ํฌ๋ฉด ๋ฃจํ”„ ์ข…๋ฃŒ
      else child ์ค‘ ํฐ ๊ฐ’์„ i์— ๋†“๋Š”๋‹ค.
      i = i//2 ๋กœ ๋‹ค์Œ ์ž์‹ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰
    parent๋ฅผ ๋นˆ ๊ณณ์— ๋†“๋Š”๋‹ค A[i//2] = parent

2. ๋ฃจํŠธ ๋…ธ๋“œ์™€ ์—”๋“œ ๋…ธ๋“œ๋ฅผ swapํ•œ๋‹ค
3. ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ์ œ์™ธํ•œ 0~N-1๊ฐœ์˜ ๋…ธ๋“œ๋ฅผ heapifyํ•œ๋‹ค.
4. heapifyํ•  ๋…ธ๋“œ๊ฐ€ ์—†์„ ๋•Œ๊นŒ์ง€ 2-3 ๋ฐ˜๋ณต

๊ธฐ๋ณธ์ ์œผ๋กœ ์„ ํƒ ์ •๋ ฌ์˜ ์‘์šฉ, ์ฆ‰ N๋ฒˆ์งธ ์›์†Œ๋งˆ๋‹ค ์ตœ๋Œ“๊ฐ’์„ ํ™•์ธํ•˜์—ฌ ์ •๋ ฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ตœ๋Œ“๊ฐ’์„ ์‚ฐ์ถœํ•˜๋Š” ๊ณผ์ •์—์„œ root = max๋ฅผ ๋ณด์žฅํ•˜๋Š” heap ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค. heapify ํ•จ์ˆ˜๋Š” ๋ณต์žก๋„๊ฐ€ O(logN)์ด๋ฏ€๋กœ, ์ „์ฒด ๋ณต์žก๋„๋Š” O(NlogN)์ด ๋œ๋‹ค.


3. What's more?

  • Decision Tree
  • Sorting by Distribution
  • Bucket Sort, Radix Sort (MSD, LSD)

'DevLog ๐Ÿ“จ > Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์œ„์ƒ ์ •๋ ฌ Topological Sorting  (0) 2023.02.14