-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmergeSort.py
More file actions
34 lines (29 loc) · 783 Bytes
/
Copy pathmergeSort.py
File metadata and controls
34 lines (29 loc) · 783 Bytes
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
import math
def merge(arr1, arr2):
c = []
i = 0
j = 0
for k in range(len(arr1)+len(arr2)):
if i < len(arr1) and j < len(arr2) and len(c) < len(arr1)+len(arr2):
if arr1[i] < arr2[j]:
c.append(arr1[i])
i += 1
else:
c.append(arr2[j])
j += 1
elif i == len(arr1):
c.append(arr2[j])
j += 1
else:
c.append(arr1[i])
i += 1
return c
def mergeSort(arr):
if len(arr) <= 2:
return merge(arr[:1], arr[1:])
else:
mid = int(math.floor(len(arr) / 2))
a = mergeSort(arr[:mid])
b = mergeSort(arr[mid:])
return merge(a, b)
print(mergeSort([2, 3, 5, 9, 1, 4, 7, 6, 8]))