-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqs_05282019.py
More file actions
executable file
·91 lines (76 loc) · 1.88 KB
/
qs_05282019.py
File metadata and controls
executable file
·91 lines (76 loc) · 1.88 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/Users/pwu/anaconda/bin/python3
""" shebang for linux #!/usr/bin/env python3
quick sort
"""
def qs(arr):
""" entry point to quick sort
"""
print("before {}".format(arr))
qqs(arr, 0, len(arr) - 1)
print("after {}".format(arr))
def qqs(arr, start, end):
""" recursive query for quick sort
"""
pivotIndex = 0
if start < end:
pivotIndex = partition(arr, start, end)
qqs(arr, 0, pivotIndex - 1)
qqs(arr, pivotIndex + 1, end)
def partition(arr, start, end):
""" find the correct spot for the pivot value
"""
pIndex = start
pVal = arr[end]
while start < end:
if arr[start] < pVal:
arr[start], arr[pIndex] = arr[pIndex], arr[start]
pIndex += 1
start += 1
arr[start], arr[pIndex] = arr[pIndex], arr[start]
return pIndex
def ms(arr):
""" entry point for merge sort
"""
print("before {}".format(arr))
mms(arr)
print("after {}".format(arr))
def mms(arr):
"""recursive query for merge sort
"""
mid = len(arr) // 2
if mid > 0:
left = arr[:mid]
right = arr[mid:]
mms(left)
mms(right)
merge(arr, left, right)
def merge(arr, left, right):
lidx = 0
ridx = 0
aidx = 0
while lidx < len(left) and ridx < len(right):
if left[lidx] < right[ridx]:
arr[aidx] = left[lidx]
lidx += 1
else:
arr[aidx] = right[ridx]
ridx += 1
aidx += 1
while lidx < len(left):
arr[aidx] = left[lidx]
lidx += 1
aidx += 1
while ridx < len(right):
arr[aidx] = right[ridx]
ridx += 1
aidx += 1
if __name__ == '__main__':
print("----------")
qs([5,3,8,9,5,3,1,10,100])
ms([5,3,8,9,5,3,1,10,100])
print("----------")
qs([5,3])
ms([5,3])
print("----------")
qs([5])
ms([5])