-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort.py
60 lines (34 loc) · 1.32 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
def merge_sort(array):
#array should contain more then 1 element
if len(array) > 1 :
mid_index = int((0 + len(array)) / 2 )
first_half = array[:mid_index]
second_half = array[mid_index:]
#Keep deividing until we reach smallest undivisable part
smallest_undivisable_part1 = merge_sort(first_half)
smallest_undivisable_part2 = merge_sort(second_half)
#Now start conquering above small parts
finalSortedOutPut = merge(smallest_undivisable_part1,smallest_undivisable_part2)
return finalSortedOutPut
else:
#Return single element array
return array
def merge(array1,array2):
sortedArray = []
while len(array1) != 0 and len(array2) != 0:
if array1[0]>array2[0]:
sortedArray.append(array2[0])
del array2[0]
else:
sortedArray.append(array1[0])
del array1[0]
#Sorting is done now simply add remainig in sorted array
while len(array1) != 0 :
sortedArray.append(array1[0])
del array1[0]
while len(array2) != 0 :
sortedArray.append(array2[0])
del array2[0]
return sortedArray
test_unsorted_array = [14, 17, 13, 15, 19, 10, 3, 16, 9, 12]
print(merge_sort(test_unsorted_array))