-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_sort.py
32 lines (27 loc) · 890 Bytes
/
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
import math
def merger_function(array1,array2):
returning_array = []
for i in range(len(array2)+len(array1)):
if (len(array1)==0):
returning_array+=array2
break
if (len(array2)==0):
returning_array+=array1
break
if (array1[0]>array2[0]):
min = array2[0]
array2 = array2[1:]
else:
min = array1[0]
array1 = array1[1:]
returning_array.append(min)
return returning_array
def breaker_function(my_array):
length = len(my_array)
if (length > 1):
return(merger_function(breaker_function(my_array[:math.floor(length/2)]),breaker_function(my_array[math.floor(length/2):])))
else:
return my_array
def merge_sort_fun(my_array):
return(breaker_function(my_array))
merge_sort_fun([4,8,9,6,5,2,3,1,7,10])