-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertion_merger_sort.py
65 lines (59 loc) · 1.52 KB
/
insertion_merger_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
from random import randint
from time import time
global limit
def merge(arr, low, mid, high):
n1 = mid - low + 1
n2 = high - mid
left = [0] * n1
right = [0] * n2
for i in range(0, n1):
left[i] = arr[low + i]
for j in range(0, n2):
right[j] = arr[mid + j + 1]
i = 0
j = 0
k = low
while i < n1 and j <n2:
if left[i] <= right[j]:
arr[k] = left[i]
k += 1
i += 1
else:
arr[k] = right[j]
k += 1
j += 1
while i < n1:
arr[k] = left[i]
i += 1
k += 1
while j <n2:
arr[k] = right[j]
j += 1
k += 1
def split(arr, low, high, limit):
if high - low < limit and high > low:
n = high - low + 1
insertionsort(arr, low, high)
elif high > low:
mid = (high + low - 1) / 2
split(arr, low, mid, limit)
split(arr, mid + 1, high, limit)
merge(arr, low, mid, high)
def mergesort(arr, n):
split(arr, 0, n, 10)
def insertionsort(arr, start, end):
for j in range(start, end + 1):
i = j - 1
key = arr[j]
while arr[i] > key and i >= start :
arr[i + 1] = arr[i]
i -= 1
arr[i + 1] = key
def main():
arr = [randint(1,10000) for i in xrange(100000)]
start = time()
mergesort(arr, len(arr) - 1)
print arr
print time() - start
if __name__ == "__main__":
main()