When we talk about an algorithm’s “memory complexity,” we need to be crystal-clear about what we’re counting:
- Total space (including the input itself)
- Auxiliary space (excluding the space needed simply to hold the input)
Depending on which definition you use, your ability to distinguish one algorithm from another can either collapse or flourish.
1. Including the input in your space count
If you say
“Space = everything—code, static data, and the input”
then for any algorithm whose input size is n, just storing that input already costs you Ω(n) space.
– Consequence: Almost every “interesting” algorithm on an n-element input will be at least O(n) space.
– Blurring of distinctions:
* A naïve mergesort (which needs O(n) extra)
* An in-place quicksort (which needs O(log n) extra)
* A streaming algorithm that processes one element at a time (which needs O(1) extra)
All of these, when you include the input, are “just O(n).” You lose the ability to say “Mergesort uses more working memory than quicksort.”
2. Excluding the input (auxiliary space)
If you instead define
“Auxiliary space = what the algorithm needs in addition to the input buffer”
then you get a far richer classification:
| Algorithm | Input space | Aux. space | Total space |
|---|
| In-place heapsort | n | O(1) | O(n) + O(1) |
| Quicksort | n | O(log n) | O(n) + O(log n) |
| Mergesort | n | O(n) | O(n) + O(n) |
| Streaming scan | n (stream?) | O(1) | O(n) + O(1) |
– We can now say
• Heapsort is “constant-space”
• Quicksort is “logarithmic auxiliary space”
• Mergesort is “linear auxiliary space”
– This is by far the most common convention in algorithm textbooks: they report “space complexity” meaning extra/auxiliary space.
Why it matters
- Practical decisions. On a memory-constrained device you might refuse to use mergesort but happily run heapsort.
- Theoretical clarity. It lets us draw finer lines between “in-place” and “out-of-place” algorithms.
- Streaming algorithms. These often assume you never store the entire input—auxiliary-space measures spotlight their O(1) or O(log n) memory usage.
Bottom line
- Including input → everybody’s Ω(n) → coarse, not very helpful for comparing algorithms’ extra needs.
- Excluding input → you can classify algorithms into O(1), O(log n), O(n), … based on their additional memory requirements.
In almost all algorithmic analyses, when people say “this runs in O(g(n)) space,” they mean auxiliary space—i.e. excluding the input buffer—so that you can meaningfully compare one algorithm’s memory overhead to another’s.