-
Notifications
You must be signed in to change notification settings - Fork 34
/
mergesort.py
43 lines (35 loc) · 1.1 KB
/
mergesort.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
"""mergesort.py - implementation of mergesort. """
__author__ = "Caleb Madrigal"
__date__ = "2015-02-13"
def merge(left, right):
if len(left) < 1:
return right
elif len(right) < 1:
return left
else:
first_left, *rest_left = left
first_right, *rest_right = right
if first_left <= first_right:
return [first_left] + merge(rest_left, right)
else:
return [first_right] + merge(left, rest_right)
def mergesort(lst):
list_len = len(lst)
if list_len < 2:
return lst
if list_len == 2:
if lst[0] <= lst[1]:
return lst
else:
return [lst[1], lst[0]]
else: # list_len > 2
split_point = list_len // 2
left = lst[0:split_point]
right = lst[split_point:]
left_sorted = mergesort(left)
right_sorted = mergesort(right)
return merge(left_sorted, right_sorted)
if __name__ == "__main__":
import unittest
testsuite = unittest.TestLoader().discover('test', pattern="*mergesort*")
unittest.TextTestRunner(verbosity=1).run(testsuite)