Fundamentals

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

Aman Jaiman
8 min read
On this page +
  1. In plain English
  2. Auxiliary space vs total space
  3. The memory ranking
  4. When memory is the deciding factor
  5. In-place sorting explained
  6. Frequently asked questions

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:

AlgorithmAuxiliary spaceIn-place?
Bubble SortO(1)Yes
Selection SortO(1)Yes
Insertion SortO(1)Yes
Shell SortO(1)Yes
Heap SortO(1)Yes
Quick SortO(log n) call stackConventionally yes
Merge SortO(n)No
Tim SortO(n)No
Counting SortO(k)No
Radix SortO(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.

Found this useful?

Share it with someone who is learning this too.

Questions

Frequently asked questions

Which sorting algorithm uses the least memory? +
Heap Sort uses the least among O(n log n) algorithms, needing only O(1) extra space. Bubble, Selection, and Insertion Sort are also O(1) but are slower at O(n²).
Why does Merge Sort need O(n) space? +
Merge Sort combines two sorted halves by copying elements into a temporary buffer the size of the input. That buffer is the O(n) auxiliary space. Truly in-place merge variants exist but are complex and slower.
Is Quick Sort in-place? +
By convention yes, though with an asterisk. Quick Sort partitions within the original array and allocates no auxiliary array, so its data movement is genuinely in-place. It does consume O(log n) stack space for recursion, which is not literally O(1) — so some texts classify it as "in-place" and stricter ones as "almost in-place". In practice the log n stack frames are negligible against an O(n) auxiliary array.
Why is Merge Sort not in-place? +
Because merging two sorted runs cannot easily be done without somewhere to put the result. The standard merge copies both halves into a temporary buffer, then writes back in order, which needs O(n) extra space. In-place merge algorithms do exist, but they trade that memory saving for either much worse constant factors or the loss of stability, which defeats the main reasons for choosing Merge Sort in the first place.

See it in motion

Watch this algorithm and nine others run step by step, with live pseudocode and comparison counters.

Launch the visualiser
Aman Jaiman
Written by
Aman Jaiman

Software engineer at a stealth-stage startup, and previously a front-end engineer for around a year and a half.

Keep reading

Related guides

Link copied