-
Notifications
You must be signed in to change notification settings - Fork 119
/
mix_sort.py
44 lines (39 loc) · 932 Bytes
/
mix_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
import math
def insertion_sort(A, p, r):
for j in xrange(p+1, r+1):
key = A[j]
i = j-1
while i >= p and A[i] > key:
A[i+1] = A[i]
i = i - 1
A[i+1] = key
def merge(A, p, q, r):
L = A[p: q+1]+[float("inf")]
R = A[q+1:r+1]+[float("inf")]
i = 0
j = 0
for k in xrange(p, r + 1):
if L[i] <= R[j]:
A[k] = L[i]
i += 1
else:
A[k] = R[j]
j += 1
def merge_sort(A, p, r):
if p < r:
q = (p+r)/2
merge_sort(A, p, q)
merge_sort(A, q+1, r)
merge(A, p, q, r)
def mixed_sort(A, p, r):
if r - p < 2:
insertion_sort(A, p, r)
else:
#q = int((p+r)/2)
#mixed_sort(A, p, q)
#mixed_sort(A, q + 1, r)
#merge(A, p, q, r)
merge_sort(A, p, r)
A = [8, 7, 6, 5, 4, 3, 2, 1]
mixed_sort(A, 0, 7)
print A