forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path5.py
More file actions
17 lines (12 loc) ยท 651 Bytes
/
5.py
File metadata and controls
17 lines (12 loc) ยท 651 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
array = [5, 7, 9, 0, 3, 1, 6, 2, 4, 8]
def quick_sort(array):
# ๋ฆฌ์คํธ๊ฐ ํ๋ ์ดํ์ ์์๋ง์ ๋ด๊ณ ์๋ค๋ฉด ์ข
๋ฃ
if len(array) <= 1:
return array
pivot = array[0] # ํผ๋ฒ์ ์ฒซ ๋ฒ์งธ ์์
tail = array[1:] # ํผ๋ฒ์ ์ ์ธํ ๋ฆฌ์คํธ
left_side = [x for x in tail if x <= pivot] # ๋ถํ ๋ ์ผ์ชฝ ๋ถ๋ถ
right_side = [x for x in tail if x > pivot] # ๋ถํ ๋ ์ค๋ฅธ์ชฝ ๋ถ๋ถ
# ๋ถํ ์ดํ ์ผ์ชฝ ๋ถ๋ถ๊ณผ ์ค๋ฅธ์ชฝ ๋ถ๋ถ์์ ๊ฐ๊ฐ ์ ๋ ฌ์ ์ํํ๊ณ , ์ ์ฒด ๋ฆฌ์คํธ๋ฅผ ๋ฐํ
return quick_sort(left_side) + [pivot] + quick_sort(right_side)
print(quick_sort(array))