forked from OliverIgnetik/Data-Structures-Algorithms-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeSort.py
More file actions
34 lines (27 loc) · 842 Bytes
/
Copy pathmergeSort.py
File metadata and controls
34 lines (27 loc) · 842 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
arr = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0]
def mergesort(arr):
if len(arr) == 1:
return arr
length = len(arr)
mid = length // 2
left = arr[:mid]
right = arr[mid:]
# print('Left {}'.format(left))
# print('Right {}'.format(right))
return merge(mergesort(left), mergesort(right))
def merge(left, right):
result = []
leftindex = 0
rightindex = 0
while leftindex < len(left) and rightindex < len(right):
if left[leftindex] < right[rightindex]:
result.append(left[leftindex])
leftindex += 1
else:
result.append(right[rightindex])
rightindex += 1
# print(left,right)
# print( result + left[leftindex:] + right[rightindex:] )
return result + left[leftindex:] + right[rightindex:]
x = mergesort(arr)
print(x)