-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMerge Sort.py
More file actions
92 lines (79 loc) · 2.65 KB
/
Merge Sort.py
File metadata and controls
92 lines (79 loc) · 2.65 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
"""
Merge Sort ☆
Write a function that takes in an array of integers and returns a sorted version of that array. Use the Merge Sort algorithm to sort the
array.
If you're unfamiliar with Merge Sort, we recommend watching the Conceptual Overview section of this question's video explanation
before starting to code.
Sample Input
array = [8, 5, 2, 9, 5, 6, 3]
Sample Output
[2, 3, 5, 5, 6, 8, 9]
"""
# SOLUTION 1
# Best: O(nlog(n)) time | O(nlog(n)) space
# Average: (nlog(n)) time | O(nlog(n)) space
# Worst: O(nlog(n)) time | O(nlog(n)) space
def mergeSort(array):
if len(array) == 1:
return array
middleIdx = len(array) // 2
leftHalf = array[:middleIdx]
rightHalf = array[middleIdx:]
return mergeSortedArrays(mergeSort(leftHalf), mergeSort(rightHalf))
def mergeSortedArrays(leftHalf, rightHalf):
sortedArray = [None] * (len(leftHalf) + len(rightHalf))
k = i = j = 0
while i < len(leftHalf) and j < len(rightHalf):
if leftHalf[i] <= rightHalf[j]:
sortedArray[k] = leftHalf[i]
i += 1
else:
sortedArray[k] = rightHalf[j]
j += 1
k += 1
while i < len(leftHalf):
sortedArray[k] = leftHalf[i]
i += 1
k += 1
while j < len(rightHalf):
sortedArray[k] = rightHalf[j]
j += 1
k += 1
return sortedArray
# SOLUTION 2
# Best: O(nlog(n)) time | O(n) space
# Average: 0(nlog(n)) time | O(n) space
# Worst: O(nlog(n)) time | O(n) space
def mergeSort(array):
if len(array) <= 1:
return array
auxiliaryArray = array[:]
mergeSortHelper (array, 0, len(array) - 1, auxiliaryArray)
return array
def mergeSortHelper(mainArray, startIdx, endIdx, auxiliaryArray):
if startIdx == endIdx:
return
middleIdx = (startIdx + endIdx) // 2
mergeSortHelper(auxiliaryArray, startIdx, middleIdx, mainArray)
mergeSortHelper (auxiliaryArray, middleIdx + 1, endIdx, mainArray)
doMerge(mainArray, startIdx, middleIdx, endIdx, auxiliaryArray)
def doMerge(mainArray, startIdx, middleIdx, endIdx, auxiliaryArray):
k = startIdx
i = startIdx
j = middleIdx + 1
while i <= middleIdx and j <= endIdx:
if auxiliaryArray[i] <= auxiliaryArray[j]:
mainArray[k] = auxiliaryArray[i]
i += 1
else:
mainArray[k] = auxiliaryArray[j]
j += 1
k += 1
while i <= middleIdx:
mainArray[k] = auxiliaryArray[i]
i += 1
k += 1
while j <= endIdx:
mainArray[k] = auxiliaryArray[j]
j += 1
k += 1