forked from ghosert/VimProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.py
More file actions
executable file
·110 lines (88 loc) · 2.39 KB
/
Copy pathsort.py
File metadata and controls
executable file
·110 lines (88 loc) · 2.39 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/python
from decimal import Decimal
infinity = Decimal('Infinity')
# Insertion sort: page 19 - 20
def insertion_sort(A):
for j, key in enumerate(A):
# Skip the first item, since it's already sorted for a single item.
if j == 0:
continue
# Insert A[j] into the sorted sequence A[0 j-1]
i = j - 1
while i >= 0 and A[i] > key:
A[i + 1] = A[i]
i = i - 1
A[i + 1] = key
# Merge: page 30
def merge(A, p, q, r):
n1 = q - p + 1
n2 = r - q
# create arrays L[1 n1 + 1] and R[1 n2 + 1]
L = [A[p + i] for i in range(n1)]
R = [A[q + 1 + j] for j in range(n2)]
# [2, 4, 5, 7, infinity]
L.append(infinity)
# [1, 2, 3, 6, infinity]
R.append(infinity)
i = 0
j = 0
n = r - p + 1
for ni in range(n):
ai = ni + p
if L[i] <= R[j]:
A[ai] = L[i]
i = i + 1
else:
A[ai] = R[j]
j = j + 1
# Merge sort: page 32
# Recursive call
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)
# Binary search: Page 36
def binary_search(A, a, p, r):
while p <= r:
p, r = binary(A, a, p, r)
return r if A[r] == a else None
def binary(A, a, p, r):
z = (p + r) / 2
if A[z] == a:
return z + 1, z
elif A[z] < a:
return z + 1, r
elif A[z] > a:
return p, z - 1
# Bubble sort: Page 37
def bubble_sort(A):
for i in range(len(A)):
j = len(A) - 1 - i
for k in range(j):
l = len(A) - 1 - k
if A[l] < A[l - 1]:
temp = A[l]
A[l] = A[l - 1]
A[l - 1] = temp
if __name__ == '__main__':
A = [5, 2, 4, 6, 1, 3]
insertion_sort(A)
print A
A = [2, 4, 5, 7, 1, 2, 3, 6]
# means li[0:4] is a sorted sub array while li[4:8] is another sorted sub array
# so we set 0, 3, 7 here.
# we need to merge sort two sorted sub arrays into one sorted array.
merge(A, 0, 3, 7)
print A
A = [5, 2, 4, 7, 1, 3, 2, 6]
merge_sort(A, 0, len(A) - 1)
print A
# Given a sorted list for binary search
A = [1, 2, 2, 3, 4, 5, 6, 7]
# print binary_search(A, 5, 0, len(A) - 1)
print binary_search(A, 1, 0, 7)
A = [5, 2, 4, 7, 1, 3, 2, 6]
bubble_sort(A)
print A