diff --git a/MergeSort.py b/MergeSort.py new file mode 100644 index 000000000..7ce144dc7 --- /dev/null +++ b/MergeSort.py @@ -0,0 +1,36 @@ +def mergeSort(alist): + print("Splitting ",alist) + if len(alist)>1: + mid = len(alist)//2 + lefthalf = alist[:mid] + righthalf = alist[mid:] + mergeSort(lefthalf) + mergeSort(righthalf) + i=0 + j=0 + k=0 + while i < len(lefthalf) and j < len(righthalf): + if lefthalf[i] < righthalf[j]: + alist[k]=lefthalf[i] + i=i+1 + else: + alist[k]=righthalf[j] + j=j+1 + k=k+1 + + while i < len(lefthalf): + alist[k]=lefthalf[i] + i=i+1 + k=k+1 + + while j < len(righthalf): + alist[k]=righthalf[j] + j=j+1 + k=k+1 + print("Merging ",alist) + +print("Enter numbers seprated by space") +s = input() +numbers = list(map(int, s.split())) +mergeSort(numbers) +