Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Merge Sort Algorithm Implimentation
# 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]:
if len(a) == 0:
c += b
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
middle = len(x)/2
a = mergesort(x[:middle])
b = mergesort(x[middle:])
return merge(a,b)
Copy link

WeiChienHsu commented Aug 27, 2017

Hello, do you need to sort your a and b lists before the first while loop? Thanks

Copy link

drunkinlove commented Sep 12, 2017

thank you, great implementation

Copy link

kamilbolka commented Jan 2, 2018

Great and simple implementation!

Copy link

mmaridev commented Apr 21, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment