-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathQuickSort.py
42 lines (33 loc) · 1.13 KB
/
QuickSort.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
40
41
42
class QuickSort:
def quicksort(self, list_a):
self.helper(list_a, 0, len(list_a) - 1)
def helper(self, list_a, first, last):
if first < last:
split_point = self.partition(list_a, first, last)
self.helper(list_a, first, split_point - 1)
self.helper(list_a, split_point + 1, last)
def partition(self, list_a, first, last):
pivot = list_a[first]
left = first + 1
right = last
done = False
while not done:
while left <= right and list_a[left] <= pivot:
left = left + 1
while list_a[right] >= pivot and right >= left:
right = right - 1
if right < left:
done = True
else:
temp = list_a[left]
list_a[left] = list_a[right]
list_a[right] = temp
temp = list_a[first]
list_a[first] = list_a[right]
list_a[right] = temp
return right
obj = QuickSort()
a = [5, 2, 4, 56, 7, 2, 43, 57]
obj.quicksort(a)
print(a)
# output = [2,2,4,5,7,43,56,57]