Data Str-Time &space Complexity
Data Str-Time &space Complexity
DATA STRUCTURES
Unit 1-Linear structures
Year/Sem : II/III
Subject Code: 1151CS102
Topic : Time and space complexity
Faculty Name : Vijitha.S
Date : 06.08.2020
School of Computing
Vel Tech Rangarajan Dr. Sagunthala R&D Institute of
Science and Technology
COURSE DETAILS
Preamble:
This course provides an introduction to the basic concepts and techniques of
Linear and nonlinear data Structures and Analyze the various algorithm.
•Prerequisite Courses:
1150CS201 Problem Solving using C
•Related Courses:
5
8/06/2020
1151CS111 Department
ComputerofNetworks
Computer Science and Engineering
COURSE OUTCOMES
Identify and explain user defined data types, linear data structures for
solving real world problems.
Data Structures-Definition
Algorithm
Algorithm Performance Analysis
Algorithm Complexity
Asymptotic notations
Time Complexity
Time Complexity of sorting algorithms
Space Complexity
and Project
Management
(SEPM)
Program=algorithm+datastructure
and Project
Management
(SEPM)
2. Space Complexity
8/06/2020 Department of Computer Science and Engineering
Algorithm complexity
Less Time
Efficient
Algorithm
Less
Memory
and Project
Management
(SEPM)
and Project
Management
(SEPM)
• Big O - O(n)
• Big Omega - Ω(n)
• Big Theta - θ(n)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
Best-case:
(SEPM)
Average-case:
and Project
Management
(SEPM)
For example:
if a > b:
return True and Project
else: Management
(SEPM)
return False
8/06/2020 Department of Computer Science and Engineering
Time complexity
Linear Time O(n):
For example:
for value in data:
print(value)
33 42 54 56 88
Step 1
and Project
Management 33 42
(SEPM) Step 2
33
Step 3
8/06/2020 Department of Computer Science and Engineering
Time complextiy
For example:
and Project
Management
(SEPM)
and Project
Management
(SEPM)
and Project
Management
(SEPM)
for example:
for x in data:
for y in data: and Project
print(x, y) Management
(SEPM)
2 4 6 5 9 8
2 4 5 6 9 8
2 4
and Project
5 6 8 9
Management
(SEPM)
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
and Project
Management
(SEPM)
2! = 2 x 1 = 2
3! = 3 x 2 x 1 = 6
4! = 4 x 3 x 2 x 1 = 24
5! = 5 x 4 x 3 x 2 x 1 = 120
6! = 6 x 5 x 4 x 3 xManagement
2 x 1 = 720
and Project
7! = 7 x 6 x 5 x 4 x(SEPM)
3 x 2 x 1 = 5.040
8! = 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 40.320
8/06/2020 Department of Computer Science and Engineering
Time complexity
#include <stdio.h>
void main()
{ O(N)
int i, n ;
scanf(“%d”,&n); and Project
for (i = 1; i <= n; i++) { Management
(SEPM)
printf("Hello Word !!!");
}
}8/06/2020 Department of Computer Science and Engineering
EXAMPLE
O(N^2)
and Project
Management
(SEPM)
Example:
Algorithm: SUM(A, B)
and Project
Step 1 - START Management
(SEPM)
Step 2 - C ← A + B + 10
Step
8/06/2020
3 - Stop Department of Computer Science and Engineering
Space complexity
1)Algorithm abc(x,y,z)
return x*y*z+(x-y):
S(P)=C+Sp
S(P) = 3 (variables)
Sp=Variable
8/06/2020 Department of Computer Science and Engineering
Space complexity
Example 2:
Algorithm sum(x,n)
{
total=0
for i1 to n do
total=total+x[i]
S(P)=C+Sp
}
S(P)=3+n Where S(P) is space complexity of
algorithm. and Project
Management
3= Fixed (SEPM)
n=Variable
8/06/2020 Department of Computer Science and Engineering
Thank You