Skip to content

Commit

Permalink
Merge pull request #252 from nitinxgamer/nitinxgamer-patch-1-3
Browse files Browse the repository at this point in the history
Create mergeSort.py
  • Loading branch information
Ananyasingh2002 authored Oct 2, 2023
2 parents 139a38e + 78b7fdb commit 3df79f1
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Project/Python Progs/mergeSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
def mergeSort(arr):
if len(arr) > 1:
mid = len(arr)//2

L = arr[:mid]

R = arr[mid:]

mergeSort(L)

mergeSort(R)

i = j = k = 0

while i < len(L) and j < len(R):
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1

while i < len(L):
arr[k] = L[i]
i += 1
k += 1

while j < len(R):
arr[k] = R[j]
j += 1
k += 1


def printList(arr):
for i in range(len(arr)):
print(arr[i], end=" ")
print()


if __name__ == '__main__':
arr = [12, 11, 13, 5, 6, 7]
print("Given array is")
printList(arr)
mergeSort(arr)
print("\nSorted array is ")
printList(arr)

0 comments on commit 3df79f1

Please sign in to comment.