0% found this document useful (0 votes)
259 views2 pages

Space Complexity of Algorithms

Space complexity is the total amount of memory space used by an algorithm including input values. It is calculated by finding the space occupied by variables in the algorithm. Constant space complexity occurs when a program does not contain loops, recursion, or function calls and uses a fixed amount of space. Linear space complexity occurs when a program contains loops and the space usage grows linearly with input size. Examples show that a program using 3 integer variables has O(1) complexity while an array of n integers results in O(n) complexity.

Uploaded by

akash kurhe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
259 views2 pages

Space Complexity of Algorithms

Space complexity is the total amount of memory space used by an algorithm including input values. It is calculated by finding the space occupied by variables in the algorithm. Constant space complexity occurs when a program does not contain loops, recursion, or function calls and uses a fixed amount of space. Linear space complexity occurs when a program contains loops and the space usage grows linearly with input size. Examples show that a program using 3 integer variables has O(1) complexity while an array of n integers results in O(n) complexity.

Uploaded by

akash kurhe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Space Complexity of Algorithms

Space complexity is the total amount of memory space used by an algorithm/program including the
space of input values for execution. So to find space complexity, it is enough to calculate the space
occupied by the variables used in an algorithm/program.

Space Complexity = Auxiliary space + Space use by input values


Important Note: The best algorithm/program should have the lease space complexity. The lesser the
space used, the faster it executes.

Example #1

Explanation: Do not misunderstand space complexity to be 1364 Kilobytes as shown in the output
image. The method to calculate the actual space complexity is shown below.

In the above program, 3 integer variables are used. The size of the integer data type is 2 or 4 bytes
which depends on the compiler. Now, lets assume the size as 4 bytes. So, the total space occupied
by the above-given program is 4 * 3 = 12 bytes. Since no additional variables are used, no extra
space is required.

Hence, space complexity for the above-given program is O(1), or constant.

Example #2

Join Our Telegram Group to Get Notifications, Study Materials, Practice test & quiz:
https://t.me/ccatpreparations Visit: https://ccatpreparation.com
Explanation:

In the above-given code, the array consists of n integer elements. So, the space occupied by the array is 4 * n.
Also we have integer variables such as n, i and sum. Assuming 4 bytes for each variable, the total space
occupied by the program is 4n + 12 bytes. Since the highest order of n in the equation 4n + 12 is n, so the
space complexity is O(n) or linear.
Summary

Space Complexity details


Big O Notation

Constant Space Complexity occurs when the program doesn’t contain any loops, recursive
O(1) functions or call to any other functions.

Linear space complexity occurs when the program contains any loops.
O(n)

Space complexity cheat sheet for algorithms

Algorithm Worst case space complexity

Bubble Sort O(1)

Selection Sort O(1)

Insertion Sort O(1)

Merge Sort O(n)

Quick Sort O(n)

Heap Sort O(1)

Radix Sort O(n + K) Where, k – range of array elements

Join Our Telegram Group to Get Notifications, Study Materials, Practice test & quiz:
https://t.me/ccatpreparations Visit: https://ccatpreparation.com

You might also like