-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_sort.py
More file actions
executable file
·54 lines (44 loc) · 1.1 KB
/
Copy pathmerge_sort.py
File metadata and controls
executable file
·54 lines (44 loc) · 1.1 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
import collections
from random import randrange
def split(arr):
if not isinstance(arr[0], collections.Iterable):
arr = [[i, ] for i in arr]
odd = arr.pop(-1) if len(arr) % 2 else None
coupled = zip(*[iter(arr)]*2)
coupled = [list(i) for i in coupled]
if odd:
coupled.append([odd, []])
return coupled
def merge(arr1, arr2):
i = j = 0
new_l = []
while True:
try:
if arr1[i] <= arr2[j]:
new_l.append(arr1[i])
i += 1
else:
new_l.append(arr2[j])
j += 1
except IndexError:
break
residue = arr1[i:] or arr2[j:]
new_l += residue
return new_l
def sort(arr):
coupled = split(arr)
i = 0
while i < len(coupled):
coupled[i] = merge(*coupled[i])
i += 1
if len(coupled) > 1:
return sort(coupled)
return coupled[0]
def test():
arr = [randrange(100) for i in range(100)]
if not sorted(arr) == sort(arr):
print sort(arr)
else:
print 'SORTED'
if __name__ == "__main__":
test()