Split the list in half until each piece has one item, then merge the sorted pieces back together. Divide and conquer.
Python · intermediate · 10 min · 74 steps · free
def merge(left, right):
result = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result
def merge_sort(items):
if len(items) <= 1:
return items
mid = len(items) // 2
left = merge_sort(items[:mid])
right = merge_sort(items[mid:])
return merge(left, right)
print(merge_sort([5, 2, 4, 1]))merge(). Its body does not run yet: it runs each time the function is called.merge_sort(). Its body does not run yet: it runs each time the function is called.merge_sort(), with items = [5, 2, 4, 1]. It gets its own names.len(items) <= 1 is false, so the block under it is skipped.mid is created with the value 2.len(items) <= 1 is false, so the block under it is skipped.mid is created with the value 1.merge_sort() #3, with items = [5]. It gets its own names.items.merge_sort() #3 has finished, so its names are gone. The value it returned, [5], is stored in left.merge_sort() #3, with items = [2]. It gets its own names.len(items) <= 1 is true, so the block under it runs.items.merge_sort() #3 has finished, so its names are gone. The value it returned, [2], is stored in right.result is created and points to a new list: [].i is created with the value 0. j is created with the value 0.i < len(left) and j < len(right) is true, so the loop body runs.result points to.j changes from 0 to 1.i < len(left) and j < len(right) is false, so the loop ends.result points to.result.extend(right[j:]).result.merge() has finished, so its names are gone.merge_sort() #2, with items = [4, 1]. It gets its own names.len(items) <= 1 is false, so the block under it is skipped.mid is created with the value 1.merge_sort() #3, with items = [4]. It gets its own names.len(items) <= 1 is true, so the block under it runs.items.merge_sort() #3 has finished, so its names are gone. The value it returned, [4], is stored in left.merge_sort() #3, with items = [1]. It gets its own names.len(items) <= 1 is true, so the block under it runs.items.merge_sort() #3 has finished, so its names are gone. The value it returned, [1], is stored in right.merge(), with left = [4] and right = [1]. It gets its own names.result is created and points to a new list: [].i is created with the value 0. j is created with the value 0.i < len(left) and j < len(right) is true, so the loop body runs.left[i] <= right[j] is false, so the block under it is skipped.result points to.j changes from 0 to 1.i < len(left) and j < len(right) is false, so the loop ends.result points to.result.extend(right[j:]).result.merge() has finished, so its names are gone.merge_sort() #2 has finished, so its names are gone. The value it returned, [1, 4], is stored in right.result is created and points to a new list: [].i is created with the value 0. j is created with the value 0.i < len(left) and j < len(right) is true, so the loop body runs.left[i] <= right[j] is false, so the block under it is skipped.result points to.j changes from 0 to 1.i < len(left) and j < len(right) is true, so the loop body runs.left[i] <= right[j] is true, so the block under it runs.result points to.i changes from 0 to 1.i < len(left) and j < len(right) is true, so the loop body runs.left[i] <= right[j] is false, so the block under it is skipped.result points to.j changes from 1 to 2.i < len(left) and j < len(right) is false, so the loop ends.result.extend(right[j:]).result.merge() has finished, so its names are gone.merge_sort() has finished, so its names are gone. It prints [1, 2, 4, 5].Answer: 3. 8 → 4 → 2 → 1.
Answer: O(n)
← Insertion Sort · Quick Sort →