Skip to content

Commit 52bdf9e

Browse files
authored
Create 5.py
1 parent eb7c179 commit 52bdf9e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

6/5.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
array = [5, 7, 9, 0, 3, 1, 6, 2, 4, 8]
2+
3+
def quick_sort(array):
4+
# 리스트가 하나 이하의 원소만을 담고 있다면 종료
5+
if len(array) <= 1:
6+
return array
7+
8+
pivot = array[0] # 피벗은 첫 번째 원소
9+
tail = array[1:] # 피벗을 제외한 리스트
10+
11+
left_side = [x for x in tail if x <= pivot] # 분할된 왼쪽 부분
12+
right_side = [x for x in tail if x > pivot] # 분할된 오른쪽 부분
13+
14+
# 분할 이후 왼쪽 부분과 오른쪽 부분에서 각각 정렬을 수행하고, 전체 리스트를 반환
15+
return quick_sort(left_side) + [pivot] + quick_sort(right_side)
16+
17+
print(quick_sort(array))

0 commit comments

Comments
 (0)