Space Complexity in Sorting Algorithms
How much extra memory does each sorting algorithm use? Learn about in-place sorts, auxiliary space, and why Merge Sort needs O(n) while Heap Sort needs O(1).
On this page +
Speed gets all the attention, but memory matters too — especially on embedded devices or when sorting huge datasets. Space complexity measures the extra memory an algorithm needs beyond the input array itself. This article explains the spectrum from O(1) in-place sorts to O(n) auxiliary-space sorts.
Auxiliary space vs total space #
Space complexity for sorting usually refers to auxiliary space — the extra memory used on top of the original array. The array itself is not counted because every algorithm needs it. An algorithm that sorts using only a few extra variables is O(1); one that allocates a second array the size of the input is O(n).
The memory ranking #
- O(1) — Bubble, Selection, Insertion, Heap Sort. They rearrange elements in place with only a handful of temporary variables.
- O(log n) — Quick Sort, from the recursion call stack.
- O(n) — Merge Sort and Tim Sort, which need a buffer to merge into.
- O(k) — Counting Sort, proportional to the range of values.
When memory is the deciding factor #
If you are sorting on a memory-constrained system, Heap Sort is attractive because it guarantees O(n log n) time with O(1) extra space. When data is too large to fit in RAM entirely, external Merge Sort wins because it streams sequential chunks from disk. The visualizer labels each algorithm's space cost so you can compare at a glance.
In-place sorting explained #
An algorithm sorts in place if it needs only a constant amount of extra memory — O(1) auxiliary space — rearranging values inside the original array instead of building a second one. The input array itself does not count towards this; the question is only how much additional space is required.
Here is where each classic algorithm lands:
| Algorithm | Auxiliary space | In-place? |
|---|---|---|
| Bubble Sort | O(1) | Yes |
| Selection Sort | O(1) | Yes |
| Insertion Sort | O(1) | Yes |
| Shell Sort | O(1) | Yes |
| Heap Sort | O(1) | Yes |
| Quick Sort | O(log n) call stack | Conventionally yes |
| Merge Sort | O(n) | No |
| Tim Sort | O(n) | No |
| Counting Sort | O(k) | No |
| Radix Sort | O(n + k) | No |
The trade-off is nearly always memory against one of two other properties. Merge Sort spends O(n) extra space and buys guaranteed O(n log n) time and stability. Heap Sort spends nothing extra and matches the time guarantee, but gives up stability to do it. Quick Sort spends only stack space and is faster in practice, but gives up the worst-case guarantee.
There is no algorithm that is simultaneously in-place, stable, and guaranteed O(n log n) with small constants. Every practical sort picks which of those to sacrifice, and knowing which one it sacrificed is most of what you need to choose between them.
Frequently asked questions
Which sorting algorithm uses the least memory? +
Why does Merge Sort need O(n) space? +
Is Quick Sort in-place? +
Why is Merge Sort not in-place? +
See it in motion
Watch this algorithm and nine others run step by step, with live pseudocode and comparison counters.
Launch the visualiser
Software engineer at a stealth-stage startup, and previously a front-end engineer for around a year and a half.