Data Structures and Algorithms • C# examples
Auxiliary Space and Memory Complexity
Account for extra storage, recursion depth, and output separately.
Concept
Space analysis distinguishes memory used by the input and required output from auxiliary memory created by the algorithm. An in-place operation may use only a constant amount of extra storage, while a recursive algorithm can consume stack space proportional to its depth. Be explicit about which convention you use.
How to approach it
List arrays, collections, recursion frames, and temporary buffers; express each in terms of input dimensions.
Complexity
An in-place partition is O(1) auxiliary space; merge sort typically uses O(n) temporary storage.
C# example
static void ReverseInPlace(int[] items)
{
for (int left = 0, right = items.Length - 1; left < right; left++, right--)
(items[left], items[right]) = (items[right], items[left]);
}
Expected result or behavior
The array is reversed using a pair of indices and constant auxiliary storage.
Practice exercise
Compare the extra space for copying an array before reversing it versus swapping in place.
Key takeaway: Time and memory are separate resources; analyze both.