From 7ef225d1e5b48b8f368fb475f9108af3fc81854c Mon Sep 17 00:00:00 2001 From: NIKITA PANDEY <113332472+nikitapandeyy@users.noreply.github.com> Date: Thu, 23 Mar 2023 22:18:43 +0530 Subject: [PATCH] Update merge_sort.py The only change made in the code is to add a few print statements to show the original and sorted list. --- Sorting Algorithms/merge_sort.py | 48 ++++++++++++++++---------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/Sorting Algorithms/merge_sort.py b/Sorting Algorithms/merge_sort.py index 73d3ab31..c9a33d54 100644 --- a/Sorting Algorithms/merge_sort.py +++ b/Sorting Algorithms/merge_sort.py @@ -4,31 +4,31 @@ import sys def merge_sort(A): - merge_sort2(A, 0, len(A)-1) - + merge_sort2(A, 0, len(A)-1) + def merge_sort2(A, first, last): - if first < last: - middle = (first + last)//2 - merge_sort2(A, first, middle) - merge_sort2(A, middle+1, last) - merge(A, first, middle, last) - + if first < last: + middle = (first + last)//2 + merge_sort2(A, first, middle) + merge_sort2(A, middle+1, last) + merge(A, first, middle, last) + def merge(A, first, middle, last): - L = A[first:middle+1] - R = A[middle+1:last+1] - L.append(sys.maxsize) - R.append(sys.maxsize) - i = j = 0 - - for k in range (first, last+1): - if L[i] <= R[j]: - A[k] = L[i] - i += 1 - else: - A[k] = R[j] - j += 1 - + L = A[first:middle+1] + R = A[middle+1:last+1] + L.append(sys.maxsize) + R.append(sys.maxsize) + i = j = 0 + + for k in range (first, last+1): + if L[i] <= R[j]: + A[k] = L[i] + i += 1 + else: + A[k] = R[j] + j += 1 + A = [5,9,1,2,4,8,6,3,7] -print(A) +print("Original List: ", A) merge_sort(A) -print(A) +print("Sorted List: ", A)