-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergesort.py
executable file
·56 lines (44 loc) · 1.26 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3
def merge_sort(arr):
# stop at last element
if len(arr) > 1:
# set the bounds on our array
# split left and right into sub-arrays
mid = int(len(arr)/2)
left = arr[:mid]
right = arr[mid:]
# recursively call on the sub-arrays
# until they are 1 element
merge_sort(left)
merge_sort(right)
# right index
ri = 0
# left index
li = 0
# input/output array index
ai = 0
# walk through left and right sub-arr
# adding the smallest to the array
while li < len(left) and ri < len(right):
if left[li] < right[ri]:
arr[ai] = left[li]
li = li + 1
else:
arr[ai] = right[ri]
ri = ri + 1
ai += 1
# append remaining elements
# on the main array so they are not
# lost/overwritten in subsequent calls
while li < len(left):
arr[ai] = left[li]
li = li + 1
ai = ai + 1
while ri < len(right):
arr[ai] = right[ri]
ri = ri + 1
ai = ai + 1
my_arr = [4, 6, 3, 9, 0, 2, 7]
print(my_arr)
merge_sort(my_arr)
print(my_arr)