Back to website
Mayank Aggarwal Learning Center

Phase 03 · DSA & Problem SolvingLesson 4 of 4

Merge Sort & Quick Sort

Two classic divide-and-conquer sorts, visualised and dry-run.

  • 33:14 video
  • 2 min read
  • Intermediate
01

The problem

Sorting is one of the most studied problems in computer science, and two algorithms come up again and again in interviews: merge sort and quick sort. Both use the same powerful strategy, divide and conquer, but they make different trade-offs.

02

The concept

Divide and conquer#

  1. Divide the problem into smaller sub-problems.
  2. Conquer each sub-problem (usually with recursion).
  3. Combine the results.

Divide and conquer

  1. Dividesplit into smaller sub-problems
  2. Conquersolve each one, usually with recursion
  3. Combinejoin the results

Merge sort#

Split the list in half until each piece has one item (a single item is already sorted), then merge sorted halves back together.

Python
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)

def merge(left, right):
    result, i, j = [], 0, 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
  • Always O(n log n): the list is halved about log n times, and each level of merging touches all n items.
  • Stable: equal items keep their original order.
  • Needs O(n) extra memory for the merged lists.

Quick sort#

Pick a pivot, move smaller items to its left and larger items to its right (partition), then sort each side.

Python
def quick_sort(items):
    if len(items) <= 1:
        return items
    pivot = items[len(items) // 2]
    smaller = [x for x in items if x < pivot]
    equal   = [x for x in items if x == pivot]
    larger  = [x for x in items if x > pivot]
    return quick_sort(smaller) + equal + quick_sort(larger)
  • Average O(n log n), and very fast in practice.
  • Worst case O(n²) when pivots are consistently bad (for example always the smallest item), making partitions very uneven.
  • The classic in-place version uses very little extra memory.

Side by side#

Merge sortQuick sort
Time (average)O(n log n)O(n log n)
Time (worst)O(n log n)O(n²)
Extra memoryO(n)O(log n) in-place
Stable?YesNot usually

Practice#

Dry-run both algorithms on paper with [5, 2, 8, 1, 9, 3]. Drawing the splits and merges by hand is the fastest way to make the idea click.

03

Try it

Your task · about 10 minutes

Implement merge sort, then compare its speed with sorted() on a list of 100,000 random numbers.

Use the interactive visual above to explore it step by step
04

Recap & next

Key takeaways

  1. Both split the problem into smaller parts, solve them, then combine (divide and conquer).
  2. Merge sort is always O(n log n) and stable, but needs extra memory for merging.
  3. Quick sort averages O(n log n) and sorts in place, but degrades to O(n²) with consistently bad pivots.
  4. Python's built-in sorted() uses Timsort, a hybrid that runs in O(n log n).

Quick check

2 questions · instant feedback

1Which sort guarantees O(n log n) in every case?
2What makes quick sort degrade to O(n²)?

Finished this lesson?

Mark it complete to track your progress and keep your streak going.

Next