-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithm.py
More file actions
44 lines (36 loc) · 890 Bytes
/
Copy pathAlgorithm.py
File metadata and controls
44 lines (36 loc) · 890 Bytes
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
def insertion_sort(v):
for j in range(1, len(v)):
key = v[j]
i = j-1
while i >= 0 and v[i] > key:
v[i+1] = v[i]
i = i-1
v[i+1] = key
def quick_sort(v, p, r):
if p < r:
q = partition(v, p, r)
quick_sort(v, p, q-1)
quick_sort(v, q+1, r)
def partition(v, p, r):
x = v[r]
i = p-1
for j in range(p, r):
if v[j] <= x:
i += 1
v[i], v[j] = v[j], v[i]
v[i+1], v[r] = v[r], v[i+1]
return i+1
def best_quick_sort(v, p, r):
if p < r:
q = best_partition(v, p, r)
best_quick_sort(v, p, q-1)
best_quick_sort(v, q+1, r)
def best_partition(v, p, r):
x = v[(p+r)//2]
i = p-1
for j in range(p, r):
if v[j] <= x:
i += 1
v[i], v[j] = v[j], v[i]
v[i+1], v[r] = v[r], v[i+1]
return i+1