Created
December 19, 2014 21:43
-
-
Save AlJohri/e255680e0990c5000425 to your computer and use it in GitHub Desktop.
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
def msort(lst): | |
if not lst: | |
return [] | |
if len(lst) == 1: | |
return lst | |
else: | |
first_half = lst[0:len(lst)/2] | |
second_half = lst[len(lst)/2:] | |
return merge(msort(first_half), msort(second_half)) | |
def merge(lsta, lstb): | |
""" | |
loop over lsta and lstb to create sorted array | |
""" | |
new_lst = [] | |
current_a_index = 0 | |
current_b_index = 0 | |
print "lsta: ", lsta | |
print "lstb: ", lstb | |
while len(new_lst) != (len(lsta) + len(lstb)): | |
if len(lsta) == current_a_index: | |
new_lst.append(lstb[current_b_index]) | |
current_b_index += 1 | |
elif len(lstb) == current_b_index: | |
new_lst.append(lsta[current_a_index]) | |
current_a_index += 1 | |
elif lsta[current_a_index] < lstb[current_b_index]: | |
new_lst.append(lsta[current_a_index]) | |
current_a_index += 1 | |
elif lsta[current_a_index] > lstb[current_b_index]: | |
new_lst.append(lstb[current_b_index]) | |
current_b_index += 1 | |
print "new list: ", new_lst | |
return new_lst | |
ques = [4,3,8,6,7,1,2,9,5] | |
sol = [1,2,3,4,5,6,7,8,9] | |
mysol = msort(ques) | |
print "" | |
print "ques: ", ques | |
print "sol: ", sol | |
print "mysol: ", mysol | |
print "correct: ", sol == mysol |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment