When you're trying to sort a list, but the bugs keep getting in the way
Here are some of the most common bugs that will ruin your day:
Don't let these bugs get the best of you! Stay vigilant, stay sorted.
def merge_sort(arr):
if len(arr) < 2:
return arr
else:
mid = len(arr) // 2
left_half = merge_sort(arr[:mid])
right_half = merge_sort(arr[mid:])
return merge(left_half, right_half)
def merge(left, right):
result = []
while left and right:
if left[0] < right[0]:
result.append(left.pop(0))
else:
result.append(right.pop(0))
left.insert(0, left.pop(-1))
result += left or right
return result