-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmergeSort.py
47 lines (40 loc) · 1.29 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
from insertionSort import insertSort
def mergeSort(arr, show=False):
'''
The show is for seeing the working of the Merge Sort, and serves no value to the sorting process.
'''
if show:
print("mergeSort called, arr=", arr)
returnedArr = arr
if len(arr)>2:
if show:
print("startingRecurssion, arr=", arr)
start = 0
end = len(arr)
mid = len(arr) // 2
if show:
print("start=",start,", end=",end,", mid=",mid)
returnedArr = mergeSort(arr[start:mid])
if show:
print("returnedArr1=", returnedArr)
returnedArr = returnedArr + mergeSort(arr[mid: end])
if show:
print("returnedArrMerged=", returnedArr)
if show:
print("Sorting happened")
arr = insertSort(returnedArr)
if show:
print("returning back, arr=", arr)
return arr
def exe():
print("Merge Sort")
arrString = input("Enter array of numbers to sort separated by space: ")
arrSplit = arrString.split(" ")
# Convertion of String type inputs into int
for i in range(0, len(arrSplit)):
arrSplit[i] = int(arrSplit[i])
# Insertion Sort Call
sortedArr = mergeSort(arrSplit)
print("Sorted Array: ", sortedArr)
if __name__ == '__main__':
exe()