forked from hyperiondev-com/Python-Debugging-Exercise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroken.py
41 lines (29 loc) · 1.12 KB
/
broken.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
from typing import List
def quick_sort(numbers:List):
quick_sort_helper( numbers, 0, len(numbers)-1)
def quick_sort_helper(numbers:List, first:int, last:int)->None:
if first<last:
splitpoint = partition(numbers, first, last)
quick_sort_helper(numbers, first, splitpoint-1)
quick_sort_helper(numbers, splitpoint+1, last)
def partition(numbers:int, first:int, last:int)->int:
pivotvalue = numbers[first]
leftmark = first + 1
rightmark = last
done = False
while not done:
while leftmark <= rightmark and numbers[leftmark] <= pivotvalue:
leftmark = leftmark + 1
while numbers[rightmark] >= pivotvalue and rightmark >= leftmark:
rightmark = rightmark -1
if rightmark <= leftmark :
done = True
else:
numbers[leftmark], numbers[rightmark] = numbers[rightmark], numbers[leftmark] # swap
# undo the swap
numbers[first] , numbers[rightmark] = numbers[rightmark], numbers[first]
return rightmark
numbers = [54, 26, 93, 17, 77, 31, 44, 55, 20]
print("Before Sort: {}".format(numbers))
quick_sort(numbers)
print("After Sort: {}".format(numbers))