๐Ÿšจ โ€˜์ด๊ฒƒ์ด ์ทจ์—…์„ ์œ„ํ•œ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋‹คโ€™ ๊ต์žฌ ์ •๋ฆฌ

์ •๋ ฌ

์ •๋ ฌ์„ ์œ„ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ๋Œ€๋ถ€๋ถ„ ์–ธ์–ด์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—๋Š” ์ด๋ฏธ ํšจ์œจ์ ์ธ ์ •๋ ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ์ง€๋งŒ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์›๋ฆฌ๋ฅผ ์ดํ•ดํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ๋„ ๊ธฐ์—… ๋ฉด์ ‘์—๋„ ๋งŽ์ด ๋“ฑ์žฅํ•˜๋Š” ๋ฌธ์ œ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ค‘์š”ํ•˜๋‹ค๊ณ  ํ•œ๋‹ค.

์„ ํƒ ์ •๋ ฌ

๊ฐ€์žฅ ๋‹จ์ˆœํ•˜๊ณ  ์ง๊ด€์ ์œผ๋กœ ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋Š” ์ •๋ ฌ ๋ฐฉ๋ฒ•์ด๋‹ค. ๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์„ ํƒํ•ด ์ฐจ๋ก€๋Œ€๋กœ ์ •๋ ฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

1
2
3
4
5
6
7
8
9
10
data = [3,6,7,1,2,9,10,4,5,8]
def selection_sort():
for i in range(len(data)):
idx = i
for j in range(i,len(data)):
if data[idx] > data[j]:
idx = j
data[idx],data[i] = data[i],data[idx]
selection_sort()
print(data)

์„ ํƒ์ •๋ ฌ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N^2)์ด๋‹ค. ํšจ์œจ์ ์ด๋ผ๊ณ ๋Š” ํ•  ์ˆ˜ ์—†๋‹ค.

์‚ฝ์ž… ์ •๋ ฌ

์‚ฝ์ž… ์ •๋ ฌ์€ ์•ž์—์„œ ๋ถ€ํ„ฐ ์ •๋ ฌํ•˜์—ฌ ๊ฐ ์›์†Œ๊ฐ€ ๋“ค์–ด๊ฐˆ ์ž๋ฆฌ๋ฅผ ์ฐพ์•„ ํ•ด๋‹น ์œ„์น˜์— ์‚ฝ์ž…ํ•˜๋Š” ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. n๋ฒˆ์žฌ ์›์†Œ๊ฐ€ ๋“ค์–ด๊ฐˆ ์œ„์น˜๋ฅผ ์ฐพ๋Š”๋‹ค๋ฉด 0 ๋ถ€ํ„ฐ n-1 ๋ฒˆ์งธ ๊นŒ์ง€์˜ ์›์†Œ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ํ™•์ธํ•œ๋‹ค. ์•ž์—์„œ๋ถ€ํ„ฐ ์ •๋ ฌ์ด ์ง„ํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์— data[:n] ์˜ ์›์†Œ๋“ค์€ ์ด๋ฏธ ์ •๋ ฌ๋œ ์ƒํƒœ์ด๋‹ค. ๋”ฐ๋ผ์„œ n๋ฒˆ์žฌ ์›์†Œ๊ฐ€ ๋“ค์–ด๊ฐˆ ์œ„์น˜๋Š” ํ•ด๋‹น ์›์†Œ๋ณด๋‹ค ํฐ ์ˆ˜๋ฅผ ๋งŒ๋‚˜ ๊ทธ ์œ„์น˜์— ๋„ฃ๋Š”๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
data = [3,6,7,1,2,9,10,4,5,8]
def insertion_sort():
for i in range(1,len(data)):
for j in range(i,0,-1):
if data[j] < data[j-1]:
data[j],data[j-1] = data[j-1],data[j]
else:
break
insertion_sort()
print(data)

for๋ฌธ์ด 2๋ฒˆ ์ค‘์ฒฉ๋˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N^2)์ด๋‹ค. ํ•˜์ง€๋งŒ ์‚ฝ์ž…์ •๋ ฌ์˜ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฑฐ์˜ ๋˜์–ด์žˆ๋Š” ์ƒํ™ฉ์—์„œ๋Š” ๋งค์šฐ ๋น ๋ฅด๊ฒŒ ๋™์ž‘ํ•œ๋‹ค. ์ตœ์„ ์˜ ๊ฒฝ์šฐ O(N)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค. ๋”ฐ๋ผ์„œ ๊ทธ๋Ÿด ๊ฒฝ์šฐ ํ€ต ์ •๋ ฌ๋ณด๋‹ค ๋น ๋ฅด๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.

ํ€ต ์ •๋ ฌ

ํ€ต ์ •๋ ฌ์€ ๊ฐ€์žฅ ๋น ๋ฅธ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ์ฃผ ์•„์ด๋””์–ด๋Š” ๊ธฐ์ค€ ๊ฐ’์„ ์ •ํ•ด ๊ทธ ๊ธฐ์ค€๋ณด๋‹ค ํฐ ๊ฐ’๊ณผ ์ž‘์€ ๊ฐ’์˜ ์œ„์น˜๋ฅผ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด๋‹ค. ์ด ๊ธฐ์ค€ ๊ฐ’์„ pivot์ด๋ผ๊ณ  ํ•œ๋‹ค.
pivot ๊ฐ’ ๋ณด๋‹ค ์ž‘์€ ๊ทธ๋ฃน๊ณผ pivot ๊ฐ’ ๋ณด๋‹ค ํฐ ๊ทธ๋ฃน์„ ๋ถ„ํ• ํ•˜๋ฉด ๋‘ ๊ทธ๋ฃน์€ pivot ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋œ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋ถ„ํ• ๋œ ๋‘ ๊ทธ๋ฃน์— ๋Œ€ํ•ด์„œ ๋‹ค์‹œํ•œ๋ฒˆ ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
pivot์„ ์–ด๋–ค ๊ฐ’์œผ๋กœ ์ •ํ•˜๋Š” ๊ฒƒ์ด ์ ์ ˆํ•œ์ง€๋Š” ๋‹ค์–‘ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋…ผ๋ฌธ์—์„œ ์„ค๋ช…ํ•˜๊ณ  ์žˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ๊ฐ’์„ pivot์œผ๋กœ ์„ค์ •ํ•˜๋Š” โ€˜ํ˜ธ์–ด ๋ถ„ํ•  ๋ฐฉ์‹โ€™์œผ๋กœ ํฌ์ŠคํŒ…์„ ํ•œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
data = [3,6,7,1,2,9,10,4,5,8]
def quick_sort(start, end):
if start >= end:
return
pivot = start
left = start+1
right = end
while left <= right:
# pivot ๋ณด๋‹ค ํฐ ๊ฐ’์ด ์™ผ์ชฝ์— ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ์ฐพ๋Š”๋‹ค
while left <= end and data[pivot] > data[left]:
left += 1
# pivot ๋ณด๋‹ค ์ž‘์€ ๊ฐ’์ด ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ์ฐพ๋Š”๋‹ค
while right > start and data[pivot] < data[right]:
right -= 1
# pivot์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ์ด ๋๋‚œ ์ƒํƒœ
if left > right:
data[pivot], data[right] = data[right], data[pivot]
else:
data[left], data[right] = data[right], data[left]
# ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ๊ทธ๋ฃน ์žฌ์ •๋ ฌ
quick_sort(start, right-1)
quick_sort(right+1, end)
quick_sort(0,len(data)-1)
print(data)

๋ถ€๋ถ„ sorting์ด ํ•œ ๋ฒˆ ๋ฐœ์ƒํ•˜๋ฉด ์ตœ์ข…์ ์œผ๋กœ right์œ„์น˜์— pivot๊ฐ’์ด ์ด๋™ํ•˜๋ฏ€๋กœ ๋‹ค์Œ ๊ทธ๋ฃน sorting์€ pivot ๊ฐ’์„ ์ œ์™ธํ•œ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ๊ทธ๋ฃน์— ์‹œํ–‰ํ•˜๋ฉด ๋œ๋‹ค.

ํŒŒ์ด์ฌ์„ ์ตœ๋Œ€๋กœ ํ™œ์šฉํ•œ ํ€ต์†ŒํŠธ ์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
data = [3,6,7,1,2,9,10,4,5,8]
def quick_sort_python(data):
if len(data) <=1:
return data
pivot = data[0]
tail = data[1:]
left = [x for x in data if x < pivot]
right = [x for x in data if x > pivot]
return quick_sort_python(left) + [pivot] + quick_sort_python(right)
print(quick_sort_python(data))

ํ€ต ์ •๋ ฌ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(NlogN) ์ด๋‹ค. ์ƒ๋‹นํžˆ ๋น ๋ฅด๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋‚ด์žฅ๋˜์–ด์žˆ๋Š” ์ •๋ ฌ ํ•จ์ˆ˜๋„ ํ€ต์†ŒํŠธ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ํ€ต์†ŒํŠธ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์ด๋ฏธ ์ •๋ ฌ๋˜์–ด์žˆ์„ ๊ฒฝ์šฐ pivot ๊ฐ’์— ๋”ฐ๋ผ ๋งค์šฐ ๋Š๋ฆฌ๊ฒŒ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ๋Š” ๋น ๋ฅธ ์ •๋ ฌ์„ ์œ„ํ•ด ์ตœ์ ์˜ pivot๊ฐ’์„ ์„ ํƒํ•จ์„ ๋ณด์žฅํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๊ฑฑ์ •ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

๊ณ„์ˆ˜ ์ •๋ ฌ

๊ณ„์ˆ˜์ •๋ ฌ์€ ๋ฐฑ์ค€์—์„œ ๋ฌธ์ œ๋ฅผ ํ’€๋‹ค๊ฐ€ ๋ช‡ ๋ฒˆ ๋ณธ์ ์ด ์žˆ๋Š”๋ฐ, (๋ฐฑ์ค€ ๋ฌธ์ œ ๋งํฌ) ๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฑ…์—์„œ ๋‹ค๋ฃจ๋Š” ๊ฒƒ์€ ๋ณด์ง€ ๋ชปํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ๋งค์šฐ ๋น ๋ฅด๊ฒŒ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•Œ์•„ ๋‘๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค. ๊ณ„์ˆ˜ ์ •๋ ฌ์€ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ ๋ฒ”์œ„๊ฐ€ ์ œํ•œ๋˜์–ด ์ •์ˆ˜ ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์„ ๋•Œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ€์žฅ ํฐ ๋ฐ์ดํ„ฐ์™€ ๊ฐ€์žฅ ์ž‘์€ ๋ฐ์ดํ„ฐ์˜ ์ฐจ์ด๊ฐ€ 1,000,000์„ ๋„˜์ง€ ์•Š์„ ๋•Œ ํšจ๊ณผ์ ์ด๋‹ค.

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์•„์ด๋””์–ด๋Š” n ๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ฐ€์žฅ ํฐ ์ˆ˜ ๋งŒํผ ํฌ๊ธฐ์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด 0์œผ๋กœ ์ฑ„์šฐ๊ณ  ๋ฐ˜๋ณต๋ฌธ์„ ํ•œ ๋ฒˆ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ํ•ด๋‹น ์ˆซ์ž์— ํ•ด๋‹นํ•˜๋Š” index๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ ์ด ๋ฐฐ์—ด์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

1
2
3
4
5
6
7
data = [3,6,7,1,2,9,10,4,5,8]
count = [0]*(max(data)+1)
for i in data:
count[i]+=1
for i in range(len(count)):
for j in range(count[i]):
print(i,end=' ')

๋งค์šฐ ๊ฐ„๋‹จํ•˜๋‹ค! ๊ณ„์ˆ˜ ์ •๋ ฌ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N+K)์ด๋‹ค. (K๋Š” ๋ฐ์ดํ„ฐ์˜ ์ตœ๋Œ€๊ฐ’) ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๊ฐ€ ๋งŽ์ด ์ค‘๋ณต๋˜์–ด ์žˆ์„ ์ˆ˜๋ก ์œ ๋ฆฌํ•˜๋‹ค.

๊ฒฐ๋ก 

์ •๋ ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ํ•ญ์ƒ O(NlogN)์„ ๋ณด์žฅํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹จ์ˆœ ์ •๋ ฌ ์ž‘์—…์—๋Š” ๊ธฐ๋ณธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ๋ฐ์ดํ„ฐ์˜ ๋ฒ”์œ„๊ฐ€ ํ•œ์ •์ ์ด๊ณ  ๋” ๋น ๋ฅด๊ฒŒ ๋™์ž‘ํ•ด์•ผ ํ•  ๋•Œ๋Š” ๊ณ„์ˆ˜ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜์ž.