问题
By time complexity we understand algorithm's running time as a function of the size of input (number of bits needed to represent the instance in memory). Then how do we define space complexity, with regard to this observation? It obviously can't be related to the size of instance...
回答1:
Space complexity can be defined in multiple ways, but the usual definition is the following. We assume that the input is stored in read-only memory somewhere, that there is a dedicated write-only memory for storing the result of the operation, and that there is some general "scratch space" memory for doing auxiliary computations. Typically, space complexity is the amount of space needed to store the output and for all the scratch space. For example, binary search has space complexity O(1) because only O(1) storage space is needed to store the input and output (assuming that array indices fit into machine words).
Sometimes, the input and output space are combined into a single storage unit and the input can be modified. In this model, for example, heapsort has space complexity O(1), while mergesort has space complexity O(n) for the auxiliary storage space needed for the merging.
Hope this helps!
来源:https://stackoverflow.com/questions/28332989/definition-of-space-complexity