Skip to content

Commit

Permalink
Merge pull request #30 from anubhav823/MergeSort
Browse files Browse the repository at this point in the history
Added MergeSort
  • Loading branch information
akshitagupta15june authored Sep 25, 2020
2 parents b76168f + 0f26573 commit e4105c3
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Sorting_Searching/mergeSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

# -*- coding: utf-8 -*-
def mergeSort(array):
if len(array)>1:

#dividing array into 2 parts
middle=len(array)//2
leftArray=array[:middle]
rightArray=array[middle:]

#recursively calling MergeSort
mergeSort(leftArray)
mergeSort(rightArray)

#3 variables for copying the sorted values into final array
i=j=k=0
#copying into final array
while i<len(leftArray) and j<len(rightArray):
if leftArray[i]<rightArray[j]:
array[k]=leftArray[i]
i+=1
else:
array[k]=rightArray[j]
j+=1
k+=1

#checking for any elements still left
while i<len(leftArray):
array[k]=leftArray[i]
i+=1
k+=1

while j<len(rightArray):
array[k]=rightArray[j]
j+=1
k+=1

0 comments on commit e4105c3

Please sign in to comment.