In-Place vs Out-of-Place Algorithms
An in-place algorithm transforms input using no or a constant amount of additional auxiliary space, typically O(1), modifying the data structure directly rather than creating a copy. Examples include Bubble Sort, Insertion Sort, and in-place variants of Quick Sort, which swap elements within the original array.
An out-of-place algorithm requires additional memory proportional to the input size, often O(n) or more, to produce its result, typically because it builds a new structure rather than modifying the original. Merge Sort is a classic example, as it requires auxiliary arrays to merge sorted halves.
In-place: O(1) or O(log n) auxiliary space (e.g., Quick Sort's recursion stack), modifies input directly
Out-of-place: O(n) or more auxiliary space, typically creates new data structures
Trade-off: in-place algorithms save memory but may sacrifice stability or simplicity
In-place algorithms are useful in memory-constrained environments like embedded systems