From 0fd98e86f1e7d793af3fe1ade44a29b965afdf24 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 23 Oct 2022 19:10:18 +0530 Subject: [PATCH] Added Mergesort algorithm --- Python/merge_sort.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Python/merge_sort.py diff --git a/Python/merge_sort.py b/Python/merge_sort.py new file mode 100644 index 0000000..710a4e9 --- /dev/null +++ b/Python/merge_sort.py @@ -0,0 +1,39 @@ +def mergeSort(myList): + if len(myList) > 1: + mid = len(myList) // 2 + left = myList[:mid] + right = myList[mid:] + + mergeSort(left) + mergeSort(right) + + i = 0 + j = 0 + + k = 0 + + while i < len(left) and j < len(right): + if left[i] <= right[j]: + + myList[k] = left[i] + + i += 1 + else: + myList[k] = right[j] + j += 1 + + k += 1 + + while i < len(left): + myList[k] = left[i] + i += 1 + k += 1 + + while j < len(right): + myList[k]=right[j] + j += 1 + k += 1 + +myList = list(map(int,input().strip().split())) +mergeSort(myList) +print(myList) \ No newline at end of file