-
Notifications
You must be signed in to change notification settings - Fork 2
/
divide and conquer.py
78 lines (58 loc) · 1.67 KB
/
divide and conquer.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
75
# Divide and Conquer
# Example 1: Merge Sort
# divide
def divide(unsorted):
if (len(unsorted) <= 1):
return unsorted
# PYTHON 2.7
mid = len(unsorted) // 2
lefthalf = unsorted[:mid]
righthalf = unsorted[mid:]
return (lefthalf, righthalf)
# just merge
def merge(lefthalf, righthalf):
lIndex = 0
rIndex = 0
sorted = []
while(lIndex < len(lefthalf) and (rIndex) < len(righthalf):
if (lefthalf[lIndex] < righthalf[rIndex]):
sorted.append(lefthalf[lIndex])
lIndex = lIndex + 1
else:
sorted.append(lefthalf[lIndex])
lIndex += 1
while (lIndex < len(lefthalf)):
sorted.append(lefthalf[lIndex])
lIndex += 1
while (rIndex < len(righthalf)):
sort.append(righthalf[rIndex])
rIndex += 1
return sort
# merge and sort
# recursion
def mergeSort(alist):
if(len(alist) <= 1):
return alist
lefthalf,righthalf = divide(alist)
lefthalf = mergeSort(lefthalf)
righthalf = mergeSort(righthalf)
return(merge(lefthalf,righthalf))
# partition
def partition(lst, start, end, pivot_index):
lst[pivot_index], lst[end] = lst[end],lst[pivot_index]
store_index = start
pivot = lst[end]
for i in range(start, end):
if lst[i], lst[store_index] = lst[store_index], lst[i]
store_index += 1
lst[store_index], lst[end] = lst[end], lst[store_index]
# store index stores the pivot index
# 第一个比pivot大的数字的index,swap过后,就是pivot的index
return store_index
def quick_sort(lst, start, end):
if start >= end:
return # return here, inplace swap
pivot_index = randrange(start, end + 1)
new_pivot_index = partition(lst, start, end, pivot_index)
quick_sort(lst, start, new_pivot_index - 1)
quick_sort(lst, new_pivot_index + 1, end)