0% found this document useful (0 votes)
66 views

Space Complexity Analysis of Iterative Algorithm & Recursive Algorithm

The document discusses space complexity analysis of iterative and recursive algorithms. It provides examples of space complexity classifications such as O(1), O(n), and O(n^2) for different iterative algorithms. For recursive algorithms, it explains space usage can be modeled as a tree and discusses tail recursion to reduce space complexity from O(n) to O(1). Finally, it briefly introduces divide and conquer algorithms like quicksort, mergesort, and binary search.

Uploaded by

MUNESH SINGH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Space Complexity Analysis of Iterative Algorithm & Recursive Algorithm

The document discusses space complexity analysis of iterative and recursive algorithms. It provides examples of space complexity classifications such as O(1), O(n), and O(n^2) for different iterative algorithms. For recursive algorithms, it explains space usage can be modeled as a tree and discusses tail recursion to reduce space complexity from O(n) to O(1). Finally, it briefly introduces divide and conquer algorithms like quicksort, mergesort, and binary search.

Uploaded by

MUNESH SINGH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Space

Complexity
Analysis of
Iterative
Algorithm &
Space Complexity Analysis of Iterative
Recursive
Algorithm Algorithm & Recursive Algorithm
Dr. Munesh
Singh

Dr. Munesh Singh

Indian Institute of Information Technology


Design and Manufacturing,
Kancheepuram
Chennai-600127

December 25, 2018


Space Compleixty

Space
Complexity
Analysis of
Iterative
Algorithm &
Recursive
Algorithm

Dr. Munesh If n will be the size of the problem, and n memory block is
Singh
required then space complexity O(n)
If n will be the size of the problem, and 10 memory block
is required then space complexity O(1)
if n will be the size of the problem, and n more memory
block is required then space complexity O(n2 )
Iterative Algorithm Space Compleixty

Space
Complexity
Analysis of
Space complexity O(1)
Iterative
Algorithm &
1 Algo(A,1,n)
Recursive
Algorithm 2 {
Dr. Munesh 3 int i;
Singh
Space complexity O(n2 )
4 for(i=1to n)
1 Algo(A,1,n)
5 A[i]=0;
2 {
6 }
3 int i,j;
4 create B[n,n];
Space complexity O(n)
5 for(i=1 to n)
1 Algo(A,1,n)
6 for(j=1 to n)
2 {
7 B[i,j]=A[i];
3 int i;
8 }
4 create B[n];
Space COmplexity of Recursive Algo

Space
Complexity
Analysis of
Iterative
Algorithm &
Tree Method (small)
Recursive
A(3)
Algorithm Tree Method (small)
Dr. Munesh
Singh 1 A(n) A(3)
2 {
A(2) pf
3 if(n>= 1)
4 { Kbits
A(1) pf Kbits
5 A(n-1);
6 pf(n); Kbits
A(0) pf Kbits
7 }
n-->O(n+1) Stack
O(n)
Space Complexity of Recursive Algo

Space
Complexity
Analysis of
Iterative
Algorithm &
Recursive Tree Method (small)
Algorithm

Dr. Munesh
1 A(n) Tree Method (small)
Singh
2 { A(3)

3 if(n>= 1) A(2) pf
A(2)

A(1) pf A(1)
4 { A(1) pf A(1)

A(0) pf A(0) A(0) pf A(0)


5 A(n-1); A(0) pf A(0) A(0) pf A(0)

A(3)=15 = 2 3+1-1 A(n)=2n+1-1


6 pf(n); A(2)=7 = 22+1-1 A(n)=O(n+1)
O(n+1)K
A(1)=3 =21+1-1
O(n)
7 A(n-1);
8 }
Divide and Conquer

Space
Complexity
Analysis of
Iterative
Algorithm &
In divide and conquer approach, the problem is hard, is
Recursive
Algorithm
divided into smaller sub-problem.
Dr. Munesh Subproblem is solved independently
Singh
Divide and Conquer

Space
Complexity Algorithms
Analysis of
Iterative
Algorithm &
Quick Sort
Recursive
Algorithm Merge Sort
Dr. Munesh
Singh
Binary Search

Difference between Linear search Vs Binary Search

You might also like