Last active
August 5, 2020 19:51
-
-
Save anirudhjayaraman/975675ce65f65b73606c to your computer and use it in GitHub Desktop.
Merge Sort Algorithm Implimentation
This file contains 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
# Code for the merge subroutine | |
def merge(a,b): | |
""" Function to merge two arrays """ | |
c = [] | |
while len(a) != 0 and len(b) != 0: | |
if a[0] < b[0]: | |
c.append(a[0]) | |
a.remove(a[0]) | |
else: | |
c.append(b[0]) | |
b.remove(b[0]) | |
if len(a) == 0: | |
c += b | |
else: | |
c += a | |
return c | |
# Code for merge sort | |
def mergesort(x): | |
""" Function to sort an array using merge sort algorithm """ | |
if len(x) == 0 or len(x) == 1: | |
return x | |
else: | |
middle = len(x)/2 | |
a = mergesort(x[:middle]) | |
b = mergesort(x[middle:]) | |
return merge(a,b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On my python 3.6 line 26 is giving a float value who causes a raise. I added an int(...) to avoid it and it works fine.