-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuick_sort.py
39 lines (39 loc) · 1.39 KB
/
Quick_sort.py
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
29
30
31
32
33
34
35
36
37
38
39
def find_pivot(input_list,first,last):
pivot = input_list[last]
left_pointer = first
right_pointer = last -1
pivot_flag = True
while pivot_flag:
if input_list[left_pointer]>pivot:
if input_list[right_pointer]<pivot:
temp=input_list[right_pointer]
input_list[right_pointer]=input_list[left_pointer]
input_list[left_pointer]=temp
right_pointer = right_pointer-1
left_pointer = left_pointer+1
else:
right_pointer = right_pointer -1
else:
if input_list[right_pointer]<pivot:
left_pointer = left_pointer + 1
else:
left_pointer = left_pointer +1
right_pointer = right_pointer -1
if left_pointer >= right_pointer:
temp = input_list[last]
input_list[last] = input_list[left_pointer]
input_list[left_pointer] = temp
pivot_flag = False
return left_pointer
def quickSort(input_list):
first = 0
last = len(input_list)-1
qsHelper(input_list,first,last)
def qsHelper(input_list,first,last):
if first<last:
partition = find_pivot(input_list,first,last)
qsHelper(input_list,first,partition-1)
qsHelper(input_list,partition+1,last)
input_list =[15,37,8,20,50,7,28,2,13]
quickSort(input_list)
print(input_list)