Space Complexity

Space complexity is the amount of memory space that an algorithm or a problem takes during the execution of that particular algorithm.

Space complexity is a parallel concept to time complexity. If we need to create an array of size n, this will require O(n) space. If we create a two-dimensional array of size n*n, this will require O(n2) space.

If we want to compare standard sorting algorithms on the basis of space, then Auxiliary Space would be a better criterion than Space Complexity. Merge Sort uses O(n) auxiliary space, Insertion sort, and Heap Sort use O(1) auxiliary space. The space complexity of all these sorting algorithms is O(n) though.

Space is required for:-

  1. Instruction space: Executable programs depend on number of lines taken to execute program.

  2. Data space: Required to store all the constant and variable values.

  3. Environment space: To store the environment information needed to resume the suspended function.