# Space Complexity in Python ## Introduction to Space Complexity

Space complexity, in the context of computer science and algorithms, refers to the measurement of how much memory or storage space an algorithm requires to solve a problem based on the size of the input.

It’s a fundamental aspect of algorithm analysis that helps us understand how efficiently an algorithm uses memory resources as the input size grows.

## Space Complexity In Python

Space Complexity is a measure of the amount of memory space that an algorithm or program requires to solve a problem as a function of the input size. It’s about understanding how much memory your code needs to execute efficiently, and it’s often expressed in terms of “big O” notation.

## The concept of space complexity

###### The concept of space complexity and break it down using an example.
• Space complexity refers to the amount of memory or storage space an algorithm requires to solve a problem as a function of the input size. It consists of two main components:
• Auxiliary Space: This refers to the extra space used by the algorithm itself, apart from the input. It includes the memory required by the variables, data structures, and other internal components of the algorithm.

• Space Used for Input Values: This is the memory required to store the input data that the algorithm operates on.

```def array_sum(arr):
total = 0
for num in arr:
total += num

input_array = [1, 2, 3, 4, 5]
result = array_sum(input_array)
print(result)
```

In this example, the array_sum function takes an array as input and calculates the sum of its elements. Let’s analyze the space complexity:

• Auxiliary Space: The algorithm uses a constant amount of extra memory regardless of the input size. The variables total and num are the main contributors here. Therefore, the auxiliary space is O(1), indicating constant space usage.

• Space Used for Input Values: The input array input_array takes up space proportional to the number of elements in the array (N). Therefore, the space used for input values is O(N), where N is the input size.

So, the overall space complexity of the array_sum algorithm is the sum of the auxiliary space and the space used for input values:

Space Complexity = O(1) (Auxiliary Space) + O(N) (Space Used for Input Values) = O(N)

### Understanding Memory Usage

Every variable, object, or data structure in your Python program consumes memory. The memory usage increases as you create more variables or allocate space for complex data structures like lists, dictionaries, or objects.

## Space Complexity Table for Some Common Algorithms In Python

Algorithm Best Case Average Case Worst Case Space Complexity
Quicksort O(log n) O(log n) O(n^2) O(log n)
Merge Sort O(n) O(n) O(n) O(n)
Bubble Sort O(1) O(1) O(1) O(1)
Insertion Sort O(1) O(n^2) O(n^2) O(1)
Selection Sort O(n^2) O(n^2) O(n^2) O(1)
Linear Search O(1) O(n) O(n) O(1)
Binary Search O(1) O(log n) O(log n) O(1)

#### Practical Examples in Python

###### 1. Fibonacci Series: A Case Study

The Fibonacci series is a classic example that’s often used to demonstrate various concepts in programming, including Order of Growth. This series starts with 0 and 1, and each subsequent number is the sum of the previous two. Let’s delve into the Fibonacci series and analyze its time complexity using Python code.

```def fibonacci_recursive(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2)

def fibonacci_iterative(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
fib = [0, 1]
for i in range(2, n + 1):
fib.append(fib[i - 1] + fib[i - 2])
return fib[n]

# Calculate the 10th Fibonacci number using both methods
n = 10
recursive_result = fibonacci_recursive(n)
iterative_result = fibonacci_iterative(n)

print(f"The {n}th Fibonacci number (Recursive): {recursive_result}")
print(f"The {n}th Fibonacci number (Iterative): {iterative_result}")
```

In this example, we’ve implemented two functions to calculate the nth Fibonacci number:

• fibonacci_recursive
• fibonacci_iterative.

The recursive approach uses the definition of the Fibonacci series directly, while the iterative approach builds up an array of Fibonacci numbers.

Analyzing the time complexity:

• The recursive approach has an exponential time complexity of O(2^n), as it recalculates the same Fibonacci numbers multiple times.
• The iterative approach has a linear time complexity of O(n), as it calculates each Fibonacci number only once.
###### 2. Sorting Algorithms and Their Order of Growth

Sorting is a fundamental operation in computer science, and various sorting algorithms have been developed to arrange elements in a specific order.

Let’s explore a couple of sorting algorithms—Bubble Sort and Quick Sort—and analyze their Order of Growth using Python code.

```def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]

# Test the Bubble Sort algorithm
arr = [64, 34, 25, 12, 22, 11, 90]
bubble_sort(arr)
print("Sorted array using Bubble Sort:", arr)

```

Analyzing the time complexity of Bubble Sort:

• In the worst case scenario, when the input array is in reverse order, Bubble Sort exhibits a quadratic time complexity of O(n^2).
• In the best case scenario, when the array is already sorted, the algorithm’s time complexity is linear, O(n).
```def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)

# Test the Quick Sort algorithm
arr = [64, 34, 25, 12, 22, 11, 90]
sorted_arr = quick_sort(arr)
print("Sorted array using Quick Sort:", sorted_arr)
```

Analyzing the time complexity of Quick Sort:

• On average, Quick Sort has a time complexity of O(n log n), making it significantly faster than Bubble Sort for larger datasets.
• In the worst case scenario, however, Quick Sort can degrade to O(n^2), particularly when the pivot selection leads to unbalanced partitions.
##### Best, Average, and Worst-Case Scenarios

An algorithm’s performance isn’t always consistent across different inputs. We’ll explore the differences between the best, average, and worst-case scenarios and why they matter.

##### Conclusion

In the world of programming, understanding Order of Growth is like having a map to navigate through complex algorithmic terrain. By grasping the intricacies of time and space complexities, developers can build applications that are not only functional but also efficient.

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others