DS Lecture 2
DS Lecture 2
Lecture 2
Time Complexity
Worst case, Best case and Average case analysis
Asymptotic Notation
Big oh notation, Omega notation, Theta notation
2
Time Complexity of Algorithms
Algorithm 1 Algorithm 2
For i=2 to N-1 For i=2 to 𝑁
if N divisible by i if N divisible by i
N is not prime N is not prime
End End
Iteration: N-1 Iteration: 𝑁-1
3
Time Complexity of Algorithms
4
Time Complexity of Algorithms
Algorithm
Machine 1
Machine 2
6
Time Complexity of Algorithms
7
Time Complexity of Algorithms
8
Time Complexity of Algorithms
1. Constant Time
𝑭(𝒏) = 𝟐 + 𝟏 = 𝟑
Function sum(X,Y)
No Effect for X, Y
result=X+Y 2
𝑶(𝟏)
return result 1 𝑂(1): Constant
End function The operation does not
depend on the size of its input
9
Time Complexity of Algorithms
2. Linear Time
3. Quadratic Time
11
Asymptotic Notations
Big O Notation
• Time taken in term of input size
• Rate of growth
12
Asymptotic Notations
13
Asymptotic Notations
For example:
The running time of one operation is computed as:
𝑓(𝑛): The running time will increase linearly with the increase in 𝑛
14
Asymptotic Notations
4 𝑶(𝟏)
9 9 9 9 9 𝑶(𝒏)
15
Asymptotic Notations
Asymptotic Notations
Following are the commonly used asymptotic notations to calculate the running time complexity of
an algorithm.
Ο Notation (big O) : used to define upper bound of an algorithm in terms of time complexity.
It describe the worst case
Ω Notation (omega) : used to define lower bound of an algorithm in terms of time
complexity. It describes the best case
θ Notation (theta) it describes the average case
16
Asymptotic Notations
Big O notation 𝑶 , omega notation 𝝮, and theta notation 𝞱 are often used to this end
Usually, asymptotic estimates are used because different implementations of the
same algorithm may differ in complexity
17
Asymptotic Notation
2. Machine independent
18
ARRAY
19
Array
Why Array?
• You want to store 5 numbers in a program
o No problem. You define five int variables: int num1, num2, num3, num4, num5;
o Easy enough, right?
o But what if you want to store 1000 numbers?
Are you really going to make 1000 separate variables?
int num1, num2,..., num998, num999, num1000;
That would be CRAZY!
Features of Array
An array is one of the most common data structures
An Array is a collection of elements of the same data type, stored at a contiguous
memory location.
An array is a data structure that is used to store multiple values of similar data
types in a contiguous memory locations under one name.
Elements of an array can be accessed using their indices.
Once an array is declared its size remains constant throughout the program.
21
Array
Array_name[index]
For example, in C++
cout<<data[4]; will display 0
• data[3] = 99; will replace -3 with 99
• data[ -1 ] illegal
• data[ 10 ] illegal (10 > upper bound)
• data[ 1.5 ] illegal
• data[ 0 ] OK
• data[ 9 ] OK
22
Array
Array Dimensionality
One dimensional (just a linear list)
5 10 18 30 45 50 60 65 70 80
23
Array
2D Array
• Given the following array (whose name is ‘M’)
20 25 60 40
30 15 70 90
• M[0][0] ?
• M[1][2] ?
• M[3][4] ?
24
Array
Array Representation
• The representation of an array can be defined by
its declaration.
• A declaration means allocating memory for an
array of a given size.
25
Array
• For example, in the next Figure the data type in the array is char, that means
each character has only one byte then
• We notice that the memory location of the first element is 200, and the
memory location of the next element is 201 .
26
Array
27
ARRAY
As a Data Structure
28
What is an Array
29
What is an Array
Homogeneity
All elements in an array must have the same data type
Contiguous Memory
Array elements (or their references) are stored in contiguous/consecutive
memory locations
Direct access to an element
Index reference is used to access it directly
Static data structure
An array cannot grow or shrink during program execution…the size is fixed
30
ARRAY
Operations
31
Array Operation
32
Array Operation
2. Traversing an array
• Display all contents of an array
All elements will be displayed
Each element will be displayed exactly once
cout<<a[i]);
}
33
Array Operation
34
Array Operation
35
Array Operation
36
Array Operation
37
Array Operation
Delete Operation
• In the delete operation, the element to be deleted is searched using the (1)
linear search, and then (2) the delete operation is performed followed by(3)
shifting the elements.
38
Array Operation
39
40