-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort.py
48 lines (41 loc) · 1.67 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
"""
|-----------------------------------------------------------|
| Merge Sort |
|-----------------------------------------------------------|
|Type || Comparison base stable sort|
|Worst case performance || O(n log n) |
|Best case performance || O(n log n) or O(n) |
|Average case performance || O(n log n) |
|Worst case space complexity || O(n) or O(log n) |
|-----------------------------------------------------------|
"""
from decorators import timethis
@timethis
def merge_sort(array):
def _sort(array):
# Actual Merge Sort Function
if len(array) == 1:
return array
else:
mid = len(array)/2
left = _sort(array[:mid])
right = _sort(array[mid:])
left_counter, right_counter, counter = 0, 0, 0
while left_counter < len(left) and right_counter < len(right):
if left[left_counter] < right[right_counter]:
array[counter] = left[left_counter]
left_counter += 1
counter += 1
else:
array[counter] = right[right_counter]
right_counter += 1
counter += 1
remaining = left if left_counter < right_counter else right
r = left_counter if remaining == left else right_counter
while r < len(remaining):
array[counter] = remaining[r]
r += 1
counter += 1
return array
# Call the Merge Sort Function
return _sort(array)