Skip to content

Instantly share code, notes, and snippets.

@ShinJJang
Last active August 29, 2015 14:25
Show Gist options
  • Save ShinJJang/1dcd06bc4a7cd39a7e8b to your computer and use it in GitHub Desktop.
Save ShinJJang/1dcd06bc4a7cd39a7e8b to your computer and use it in GitHub Desktop.
fail merge sort
def merge(array1, array2):
a_index = 0
b_index = 0
result = []
# wrong while condition
while a_index < len(array1) and b_index < len(array2):
if array1[a_index] > array2[b_index]:
result.append(array2[b_index])
b_index+=1
else:
result.append(array1[a_index])
a_index+=1
return result
a = [1, 2, 5]
b = [3, 4, 6]
print(merge(a, b))
# [1, 2, 3, 4, 5]
@ShinJJang
Copy link
Author

What is wrong:

  1. wrong while condition : in this condition, if one array index reach to end of array, while is stop in spite of remains of another array
  2. check when one array ended in advance : remains of another array must be added to result

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