-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.py
More file actions
107 lines (84 loc) · 3.62 KB
/
MergeSort.py
File metadata and controls
107 lines (84 loc) · 3.62 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Let's implement Mergesort! This is a complex problem
# because it applies recursion to sorting algorithms, but
# it's also by far the most efficient sorting algorithm we'll
# cover.
# First, we need a function definition: MergeSort should take
# as input one list.
def mergesort(lst):
# Then, what does it do? mergesort should recursively
# run mergesort on the left and right sides of lst until
# it's given a list only one item. So, if lst has only
# one item, we should just return that one-item list.
if len(lst) <= 1:
return lst
# Otherwise, we should call mergesort separately on the
# left and right sides. Since each successive call to
# mergesort sends half as many items, we're guaranteed
# to eventually send it a list with only one item, at
# which point we'll stop calling mergesort again.
else:
# Floor division on the length of the list will
# find us the index of the middle value.
midpoint = len(lst) // 2
# lst[:midpoint] will get the left side of the
# list based on list slicing syntax. So, we want
# to sort the left side of the list alone and#assign the result to the new smaller list left.
left = mergesort(lst[:midpoint])
# And same for the right side.
# #So, left and right now hold sorted lists of
# each half of the original list. They might
# each have only one item, or they could each
# have several items.
right = mergesort(lst[midpoint:])
# Now we want to compare the first items in each
# list one-by-one, adding the smaller to our new
# result list until one list is completely empty.
# If the first number in left is lower, add
# it to the new list and remove it from left
newlist = []
while len(left) and len(right) > 0:
if left[0] < right[0]:
newlist.append(left[0])
del left[0]
# Otherwise, add the first number from right
# to the new list and remove it from right
else:
newlist.append(right[0])
del right[0]
# When the while loop above is done, it means
# one of the two lists is empty. Because both
# lists were sorted, we can now add the remainder
# of each list to the new list. The empty list
# will have no items to add, and the non-empty
# list will add its items in order.
newlist.extend(left)
newlist.extend(right)
# newlist is now the sorted version of lst! So,
# we can return it. If this was a recursive call
# to mergesort, then this sends a sorted half-
# list up the ladder. If this was the original
# call, then this is the final sorted list.
return newlist
print(mergesort([2, 5, 3, 8, 6, 9, 1, 4, 7]))
print()
print()
# showing descending sorted list...for merge sort, just change left < right to left> right
def mergesort(lst1):
if len(lst1) <= 1:
return lst1
else:
midpoint = len(lst1) // 2
left = mergesort(lst1[:midpoint])
right = mergesort(lst1[midpoint:])
newlist = []
while len(left) and len(right) > 0:
if left[0] > right[0]:
newlist.append(left[0])
del left[0]
else:
newlist.append(right[0])
del right[0]
newlist.extend(left)
newlist.extend(right)
return newlist
print(mergesort([2, 5, 3, 8, 6, 9, 1, 4, 7]))