Skip to content

Comparing insertion, merge and quick sort

Question

Describe how insertion sort, merge sort and quicksort work and compare their time and space complexity.

Short answer

Insertion sort is O(n2)O(n^2), merge sort O(nlog⁡n)O(n\log n) always, quicksort O(nlog⁡n)O(n\log n) on average. Insertion sort inserts each element into the sorted prefix: O(n)O(n) when nearly sorted, in place. Merge sort splits in half, sorts each and merges: guaranteed and stable but needs O(n)O(n) extra memory. Quicksort partitions around a pivot and recurses: fast in practice and in place, but O(n2)O(n^2) with bad pivots.

Adapted from Zhou, A Practical Guide to Quantitative Finance Interviews, sec. 7.1 Algorithms, 'Sorting algorithm', p.173; question and answer rewritten by SelfMadeQuant.com.

See the hint and the full worked solution in the question bank (free account).

Open the worked solution

Lessons behind this question