Created
April 15, 2020 20:13
-
-
Save ShamsAnsari/db1c497dc251c812d1b29b576c04660a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def merge_sort(arr): | |
if len(arr) >1: | |
mid = len(arr)//2 #Finding the mid of the array | |
L = arr[:mid] # Dividing the array elements | |
R = arr[mid:] # into 2 halves | |
mergeSort(L) # Sorting the first half | |
mergeSort(R) # Sorting the second half | |
i = j = k = 0 | |
# Copy data to temp arrays L[] and R[] | |
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 | |
# Checking if any element was left | |
while i < len(L): | |
arr[k] = L[i] | |
i+=1 | |
k+=1 | |
while j < len(R): | |
arr[k] = R[j] | |
j+=1 | |
k+=1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment