-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw02.py
More file actions
28 lines (20 loc) · 976 Bytes
/
Copy pathhw02.py
File metadata and controls
28 lines (20 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#-*-coding: utf-8-*-
def partition(list, start, end):
i_index = start #왼쪽부터 pivot보다 큰 원소 찾는 인덱스
j_index = end #오른쪽부터 pivot보다 작은 원소 찾는 인덱스
pivot = list[start] #첫번째 원소를 pivot으로
while(i_index<=j_index):
if(list[i_index]<=pivot): #pivot보다 큰 원소 나타날 때까지 i_index 증가
i_index+=1
if(list[j_index]>=pivot): #pivot보다 작은 원소 나타날 때까지 j_index 감소
j_index-=1
list[i_index], list[j_index] = list[j_index], list[i_index]
#i 인덱스가 j 인덱스보다 커지면 i 인덱스 원소와 pivot 원소 교환
#def quick_sort(list, start, end)
# if(start<end)
if __name__=="__main__":
list = [26, 5, 37, 1, 61, 11, 59, 15, 48, 19]
partition(list, 0, len(list)-1)
for i in range (len(list)):
print list[i],
#quick_sort(list, 0, len(list))