-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_sort.py
More file actions
executable file
·74 lines (51 loc) · 1.95 KB
/
merge_sort.py
File metadata and controls
executable file
·74 lines (51 loc) · 1.95 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
def merge_sorted_list(alist, blist):
""" Use merge sort to sort sorted alist and blist, and return merged sorted list of alist. """
if not isinstance(alist, list):
raise TypeError("{} should be of type list".format(alist))
if not isinstance(blist, list):
raise TypeError("{} should be of type list".format(blist))
sorted_list = []
alist_length = len(alist)
blist_length = len(blist)
i = 0
j = 0
while (i < alist_length) or (j < blist_length):
if i == alist_length:
sorted_list.append(blist[j])
j = j + 1
elif j == blist_length:
sorted_list.append(alist[i])
i = i + 1
elif alist[i] <= blist[j]:
sorted_list.append(alist[i])
i = i + 1
else:
sorted_list.append(blist[j])
j = j + 1
return sorted_list
def merge_sort_list(alist):
""" Use merge sort to sort alist and return sorted list of alist. """
if not isinstance(alist, list):
raise TypeError("{} should be of type list".format(alist))
length = len(alist)
if length == 2:
if alist[0] > alist[1]:
alist[0], alist[1] = alist[1], alist[0]
return alist
elif length == 1:
return alist
half_length = length // 2
first_half_list = alist[:half_length]
second_half_list = alist[half_length:]
sorted_list = merge_sorted_list(merge_sort_list(first_half_list), merge_sort_list(second_half_list))
return sorted_list
def test_merge_sort():
""" Test driver using merge sort to sort unsorted list into sorted one. """
source_list = [3, 2, 1, 5, 6, 10, 9, 8, 4, 7, 0]
print("source list before sort = {}".format(source_list))
sorted_list = merge_sort_list(source_list)
assert sorted_list == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("sorted_list = {}".format(sorted_list))
if __name__ == "__main__":
test_merge_sort()