-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.py
74 lines (56 loc) · 1.38 KB
/
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
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
def selection_sort(A, i = None):
if i is None: i = len(A) - 1
if i == 0: return
for j in range(i):
if A[j] > A[i]:
A[j], A[i] = A[i], A[j]
selection_sort(A, i - 1)
# insertion_sort
def insertion_sort(A, i = None):
if i is None: i = len(A) - 1
if i == 0: return
for j in range(i):
if A[j] > A[i]:
A[j], A[i] = A[i], A[j]
insertion_sort(A, i - 1)
# merge_sort
def merge_sort(A, a=0, b = None):
if b is None: b = len(A)
if b - a <= 1: return
m = (a + b) // 2
merge_sort(A, a, m)
merge_sort(A, m, b)
merge(A, a, m, b)
def merge(A, a, m, b):
L = A[a:m]
R = A[m:b]
i =len(L)
j = len(R)
if a < b:
if (j <= 0) or (i > 0 and L[i - 1] > R[j - 1]):
A[b - 1] = L[i - 1]
i -= 1
else:
A[b - 1] = R[j - 1]
j -= 1
merge(A, a, m, b - 1)
# quick_sort
def quick_sort(A, a=0, b=None):
if b is None: b = len(A)
if b - a <= 1: return
m = partition(A, a, b)
quick_sort(A, a, m)
quick_sort(A, m, b)
def partition(A, a, b):
x = A[b - 1]
i = a
for j in range(a, b - 1):
if A[j] <= x:
A[i], A[j] = A[j], A[i]
i += 1
A[i], A[b - 1] = A[b - 1], A[i]
return i
if __name__ == "__main__":
A = [1, 2, 3, -4, 10, -5, -9, 101, 32]
quick_sort(A)
print(A)