-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort.py
84 lines (60 loc) · 1.85 KB
/
merge_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def split(lst):
"""
Divide the unsorted list at midpoint into sublists
Returns two(2) sublists - left and right
"""
mid = len(lst)//2 # floor division
left = lst[:mid]
right = lst[mid:]
return left, right
def merge(left, right):
"""
Merges two(2) lists, and sorting them in the process
Returns a new merged list
"""
sorted_list = []
i = 0 # i to trsck the index of the left list
j = 0 # j to track the list of the right list
# comparing values in each list
while i < len(left) and j < len(right):
if left[i] < right[j]:
sorted_list.append(left[i])
i += 1
else:
sorted_list.append(right[j])
j += 1
# if the length of left list is greater than that of right list
while i < len(left):
sorted_list.append(left[i])
i += 1
# if the length of right list is greater than that of left list
while j < len(right):
sorted_list.append(right[j])
j += 1
return sorted_list
def merge_sort(lst):
# if the lst contains no element or one(1) element
if len(lst) <= 1:
return lst
left_half, right_half = split(lst)
left = merge_sort(left_half)
right = merge_sort(right_half)
return merge(left, right)
def verify_sorted(lst):
"""
Compare the first two(2) element of the list and calling
the new fuction recursively to compare the first two element
until the base case
Returns boolean
"""
n = len(lst)
if n == 0 or n == 1:
return True
return lst[0] < lst[1] and verify_sorted(lst[1:])
alist = [54,62,93,17,77,31,44,55,20]
print("Verify unsorted list: ", verify_sorted(alist))
print("Unsorted list is: ", alist)
print("\n")
l = merge_sort(alist)
print("Verify sorted list: ", verify_sorted(l))
print("Sorted list is: ", l)