-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort_copy.py
68 lines (57 loc) · 1.44 KB
/
merge_sort_copy.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
"""
Merge sort in a new tuple
Instead of doing it in place, we create new tuples
"""
from math import inf
def merge(first, second):
"""
merges two sorted iterables from items in place
>>> merge((1, 3), (2, 4))
(1, 2, 3, 4)
>>> merge((2,), (1,))
(1, 2)
>>> merge((1, 5), (3,))
(1, 3, 5)
"""
left = first + (inf,)
right = second + (inf,)
l_index = 0
r_index = 0
# We could use tuples, here a mutable list fits better
result = []
for i in range(len(first) + len(right) - 1):
if left[l_index] < right[r_index]:
result.append(left[l_index])
l_index += 1
else:
result.append(right[r_index])
r_index += 1
return tuple(result)
def merge_sort(items):
"""
Non recursive merge sort.
>>> merge_sort([4, 3, 2, 1, 0])
(0, 1, 2, 3, 4)
>>> merge_sort([5, 1, 2, 4])
(1, 2, 4, 5)
"""
length = len(items)
if length < 2:
return items
tuples = ((i,) for i in items)
while length != 1:
new_result = []
skip = True
previous = None
for i in tuples:
if skip:
skip = False
previous = i
else:
new_result.append(merge(previous, i))
skip = True
if not skip:
new_result.append(previous)
length = len(new_result)
tuples = new_result
return tuples[0]