Computer Science/Algorithms

understanding pesudo-code of QuickSort

metamong 2022. 12. 5.

๐Ÿฆข ๋ฐฑ์ค€ ํ€ต ์ •๋ ฌ ๋Œ€ํ‘œ ๋ฌธ์ œ ๋ชจ์Œ) ์ถ”ํ›„

๐Ÿฆข ํ€ต ์ •๋ ฌ 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

๋Œ“๊ธ€