How does the sorting algorithm have a spatial complexity of O (1)?
I've been studying various sorting algorithms and their time and space complexities and have seen algorithms like bubble sort and bubble sort have O (1) spatial complexity.
This struck me as odd, because perhaps the lowest possible space complexity would be O (n) (as in memory needed to store a dataset and nothing else)?
source to share
The sorting algorithm has a spatial complexity of O (1) by allocating a constant amount of space, such as multiple variables to iterate over, etc., that are not proportional to the size of the input.
An example of a sorting algorithm that is not O (1) in terms of space would be most mergesort implementations that allocate an auxiliary array, making it O (n). Quicksort might look like O (1) in theory, but the call stack is considered to be space and is therefore called O (log n).
Examples of sorting algorithms with O (1) spatial complexity include: selection sort, insertion sort, shell sort, and heapsort.
source to share