Skip to content

QuizNot attempted

Sorting trade-offs

Which sort guarantees O(n log n) in the worst case AND is stable?
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • Merge sort
  • Merge sort always splits in half and merges in linear time, so it is O(n log n) worst case, and merging takes from the left half on ties, so it is stable (cost: O(n) extra space).
  • Not quick sort: O(n²) worst case with bad pivots, and not stable.
  • Not selection sort: O(n²) always and not stable.
  • Not heap sort: O(n log n) worst case but heap operations reorder equal keys, so not stable.
  • V8 uses TimSort
  • since ES2019 Array.prototype.sort must be stable.