๐ฆข ๋ฐฑ์ค ํต ์ ๋ ฌ ๋ํ ๋ฌธ์ ๋ชจ์) ์ถํ
๐ฆข ํต ์ ๋ ฌ concepts ํฌ์คํ ) ์ถํ
๐ฆข QuickSort pseudo-code
quick_sort(A[p..r]) { # A[p..r]์ ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ๋ค.
if (p < r) then {
q <- partition(A, p, r); # ๋ถํ
quick_sort(A, p, q - 1); # ์ผ์ชฝ ๋ถ๋ถ ๋ฐฐ์ด ์ ๋ ฌ
quick_sort(A, q + 1, r); # ์ค๋ฅธ์ชฝ ๋ถ๋ถ ๋ฐฐ์ด ์ ๋ ฌ
}
}
partition(A[], p, r) {
x <- A[r]; # ๊ธฐ์ค์์ - pivot์ rightmost ์ค์
i <- p - 1; # i๋ x๋ณด๋ค ์๊ฑฐ๋ ์์ ์์๋ค์ ๋์ง์
for j <- p to r - 1 # j๋ ์์ง ์ ํด์ง์ง ์์ ์์๋ค์ ์์ ์ง์
if (A[j] ≤ x) then A[++i] <-> A[j]; # i๊ฐ ์ฆ๊ฐ ํ A[i] <-> A[j] ๊ตํ
if (i + 1 != r) then A[i + 1] <-> A[r]; # i + 1๊ณผ r์ด ์๋ก ๋ค๋ฅด๋ฉด A[i + 1]๊ณผ A[r]์ ๊ตํ
return i + 1;
}
๐ฆข ๋ํ ์์)
๋ฐฐ์ด [5,8,1,3,7,9,2]
โป rightmost element๋ฅผ pivot์ผ๋ก ์ค์ โป
โ pseudo-code quick_sort)
โถ 1๋ฒ์งธ quick_sort(A,0,6) [5,8,1,3,7,9,2]
→ partition: rightmost 2๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌ - [1,2,5,3,7,9,8]
→ quick_sort(A,0,0) & quick_sort(A,2,6)
โป [2] ์๋ฃ โป
โถ 2๋ฒ์งธ quick_sort(A,0,0) [1] (1 quick sort ์ข์ธก)
→ p<r if๋ฌธ ์กฐ๊ฑด ๋ง์กฑํ์ง ์์ผ๋ฏ๋ก ๋!
โป [1,2] ์๋ฃ โป
โถ 3๋ฒ์งธ quick_sort(A,2,6) [5,3,7,9,8] (1 quick sort ์ฐ์ธก)
→ partition: rightmost 8์ ๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌ - [5,3,7,8,9]
โป [1,2,8] ์๋ฃ โป
โถ 4๋ฒ์งธ quick_sort(A,0,2) [5,3,7] (3 quick sort ์ข์ธก)
→ partition: rightmost 7์ ๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌ - [5,3,7]
โป [1,2,7,8] ์๋ฃ โป
โถ 5๋ฒ์งธ quick_sort(A,0,1) [5,3] (4 quick sort ์ข์ธก) / 4 quick sort ์ฐ์ธก ์์
→ partition: rightmost 3์ ๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌ - [3,5]
โป [1,2,3,7,8] ์๋ฃ โป
โถ 6๋ฒ์งธ quick_sort(A,1,1) [5] (5 quick sort ์ฐ์ธก) / 5 quick sort ์ข์ธก ์์
→ p<r if๋ฌธ ์กฐ๊ฑด ๋ง์กฑํ์ง ์์ผ๋ฏ๋ก ๋!
โป [1,2,3,5,7,8] ์๋ฃ โป
โถ 7๋ฒ์งธ quick_sort(A,1,1) [9] (3 quick sort ์ฐ์ธก)
→ p<r if๋ฌธ ์กฐ๊ฑด ๋ง์กฑํ์ง ์์ผ๋ฏ๋ก ๋!
โป [1,2,3,5,7,8,9] ์๋ฃ โป
โก pseudo-code partition) rightmost ์์๋ฅผ pivot์ผ๋ก ์ค์ ํ๊ณ pivot ๊ธฐ์ค ์ผ์ชฝ๊ณผ ์ค๋ฅธ์ชฝ ๋ฐฐ์ด ๋๋ ๋ค์ pivot ์์น ์ถ๋ ฅ
โถ ex) ์ 1๋ฒ์งธ quicksort์์ rightmost element๋ฅผ pivot์ผ๋ก ๋๊ณ partitionํ๋ ๊ณผ์ ์ ์ ๋ฆฌํด๋ณด์๋ฉด
[5,8,1,3,7,9,2] → [1,2,5,3,7,9,8]
โ pivot ์ค์ ) ์์ p, ๋ r, ๋น๊ต๋์์ด ๋๋ pivot์ rightmost element๋ก A[r]์ 2
โก i ์ค์ ) i๋ ์์ element p์ธ 5 ์์์๋ถํฐ ์์ (์ฌ๊ธฐ์์ i๋ pivot๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ์๋ค์ ๋ ์์น)
โข j ์์) j๋ ์์ element p์ธ 5๋ถํฐ ๋ element r -1์ธ, ๋ ๋ฐ๋ก ์๋ถ๋ถ 9๊น์ง ์ํ
→ A[j]๊ฐ A[r]๋ณด๋ค ๊ฐ๊ฑฐ๋ ์๋ค๋ฉด, A[++i]์ A[j]๋ฅผ ๋ฐ๊พผ๋ค. (A[j]๊ฐ pivot๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ผ๋ฏ๋ก ์ต๋ํ ์ผ์ชฝ์ผ๋ก ์น์ฐ์น๊ฒ๋ ํ๊ธฐ ์ํด)
โฃ ๋ง์ง๋ง pivot๊ณผ i ๋น๊ต) j๊ฐ ๋ค ๋๊ณ ๋ ๋ค, i๊ฐ 1์ด ๋ํด์ง ๊ฒฐ๊ณผ์ ์์น๊ฐ pivot์์น๊ฐ ์๋๋ผ๋ฉด, pivot๋ณด๋ค ํฐ ์์๊ฐ ๋ถ๋ช ํ ์์๋ค๋ ๋ป์ด ๋๋ฏ๋ก, pivot์ i+1 ์์น์ ๋ฐ๊ฟ์ฃผ์ด, pivot ๊ธฐ์ค ์ผ์ชฝ์ ๊ฐ๊ฑฐ๋ ์์ ์์๋ค, ์ค๋ฅธ์ชฝ์ ํฐ ์์๋ค๋ก ์ญ ๊ตฌ์ฑ๋๊ฒ ํ๋ค.
โค ์ต์ข ์ ์ผ๋ก pivot์ ์์น๋ฅผ return)
๐ฆข QuickSort pseudo-code - python code
# Python program for implementation of Quicksort Sort
# This implementation utilizes pivot as the last element in the nums list
# It has a pointer to keep track of the elements smaller than the pivot
# At the very end of partition() function, the pointer is swapped with the pivot
# to come up with a "sorted" nums relative to the pivot
# Function to find the partition position
def partition(array, low, high):
# choose the rightmost element as pivot
pivot = array[high]
# pointer for greater element
i = low - 1
# traverse through all elements
# compare each element with pivot
for j in range(low, high):
if array[j] <= pivot:
# If element smaller than pivot is found
# swap it with the greater element pointed by i
i = i + 1
# Swapping element at i with element at j
(array[i], array[j]) = (array[j], array[i])
# Swap the pivot element with the greater element specified by i
(array[i + 1], array[high]) = (array[high], array[i + 1])
# Return the position from where partition is done
return i + 1
# function to perform quicksort
def quickSort(array, low, high):
if low < high:
# Find pivot element such that
# element smaller than pivot are on the left
# element greater than pivot are on the right
pi = partition(array, low, high)
# Recursive call on the left of pivot
quickSort(array, low, pi - 1)
# Recursive call on the right of pivot
quickSort(array, pi + 1, high)
data = [1, 7, 4, 1, 10, 9, -2]
print("Unsorted Array")
print(data)
size = len(data)
quickSort(data, 0, size - 1)
print('Sorted Array in Ascending Order:')
print(data)
'''
Unsorted Array
[1, 7, 4, 1, 10, 9, -2]
Sorted Array in Ascending Order:
[-2, 1, 1, 4, 7, 9, 10]
'''
* ์ธ๋ค์ผ ์ถ์ฒ) HackerRank
* pseudo-code ์ค๋ช ์ถ์ฒ) https://youtu.be/COk73cpQbFQ
* python code ์ถ์ฒ) https://www.geeksforgeeks.org/python-program-for-quicksort/
'Computer Science > Algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
bitmasking (0) | 2022.12.20 |
---|---|
๐ Binary Search ๐ (2) | 2022.12.06 |
DFS / BFS (0) | 2022.10.14 |
๐งญRecursion (0) | 2022.09.27 |
Implementation (1) | 2022.09.21 |
๋๊ธ