Skip to content

Commit

Permalink
Merge pull request #96 from BEAST-PRINCE/main
Browse files Browse the repository at this point in the history
Adding Merge Sort in Python.
  • Loading branch information
Chitresh-code authored Dec 3, 2023
2 parents bca27c1 + 26cd186 commit f787a75
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions DSA_Codesheet/Python/merge_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
def merge(arr, l, m, r):

n1 = m - l + 1
n2 = r - m
L = [0] * n1
R = [0] * n2

for i in range(0, n1):
L[i] = arr[l + i]

for j in range(0, n2):
R[j] = arr[m + 1 + j]

i = 0
j = 0
k = l
while i < n1 and j < n2:
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1

while i < n1:
arr[k] = L[i]
i += 1
k += 1

while j < n2:
arr[k] = R[j]
j += 1
k += 1


def merge_sort(arr, l, r):

if l < r:
m = (l + r) // 2
merge_sort(arr, l, m)
merge_sort(arr, m + 1, r)
merge(arr, l, m, r)


arr = input("Enter a list of numbers: ").split(" ")

arr = [int(i) for i in arr]

merge_sort(arr, 0, len(arr) - 1)

for i in arr:
print(i,end=' ')

0 comments on commit f787a75

Please sign in to comment.