Sorting

Problem shape

Intermediate
4
8
15
16
23
42
Core idea

Divide the array, sort each half, then merge sorted halves.

Step 1 / 5
Problem shape
Current lesson

Merge Sort Lab

Sorting Intermediate

Merge Sort

Welcome to the Merge Sort algorithm lab. Divide the array, sort each half, then merge sorted halves. Use it when: Use when stable sorting and predictable O(n log n) time matter. The time complexity is O(n log n), and the space complexity is O(n). The pseudocode is: sort(left, right) if left >= right return mid = (left + right) / 2 sort(left, mid) sort(mid + 1, right) merge(left, mid, right) As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(n log n)

Space

O(n)

Pseudocode

sort(left, right)
  if left >= right return
  mid = (left + right) / 2
  sort(left, mid)
  sort(mid + 1, right)
  merge(left, mid, right)
Open deeper structure lesson
1.0x