You are on page 1of 16

Data Structure

Using C++Programming

Lecture 1 Part A
Introduction
Dr. Doaa Hany Elsayed
doaa.hani@hotmail.com

1-1
1
Data Structure

• It is a way to organize data.


• After organizing data it becomes easy to
process it.
• Different kinds of data structures are suited
to different kinds of applications.

1-2
2
Types of Data Structure

Types of Data Structure

Linear data structure Non-linear data structure

• Array


Linked List
• Tree

Stack
• Queue Graph

1-3
3
Introduction to algorithms
• An algorithm is a set of instructions to perform a task to solve a given
problem.
• There are several different algorithms to solve a given problem
• Analysis of algorithm deals in finding best algorithm which runs fast and
takes in less memory.

1-4
4
Analysis of Algorithms

Algorithm 1 Algorithm 2

int FindSum (int n) int FindSum (int n)


{ {
return n*(n+1)/2; int sum =0;
} for(int i=1; i<=n; i++)
sum=sum+i;
return sum;
}

1. Time complexity: how much time this algorithms are taken to complete.
2. Space complexity: how much memory this algorithms are taken to complete. 1-5
5
Algorithm analysis
• Algorithm analysis helps in evaluating performance of an algorithm in
terms of input size and its increase.
• Using algorithm analysis we don’t measure actual running time of
algorithm.
• It helps in determining how time and space taken by algorithm increase
with input size.

1-6
6
Algorithm analysis Notation
• Algorithm analysis notations are the mathematical tools used to describe the running time
of an algorithm in terms of input size.
• Example: performance of car in 1 liter of petrol
• Highway(min Traffic): 25Km/Liter
• City(max liter):15 km/liter
• City + Highway (avg traffic): 20Km/liter
• Algorithm analysis notation help us in determining:
• Best case
• Average case
• Worst case

1-7
7
Types of Algorithm Notation
• There are three notations for performing runtime analysis of an algorithm:
• Omega (Ω) Notation
• Big O (O) Notation
• Theta (Θ) Notation

1-8
8
Omega (Ω) Notation
• It is formal way to express the lower bound of an algorithm’s running time.
• Lower bound means for a give input this notation determines best amount of time an
algorithm can take to complete.
• For example:
• If we say certain algorithm takes 100 secs as best amount of time. So, 100 secs will
be lower bound of that algorithm. The algorithm can take more than 100 secs but it
will not take less than 100 secs.

1-9
9
Big O (O) Notation
• It is formal way to express the Upper bound of an algorithm’s running time.
• Upper bound means for any give input this notation determines longest amount of
time an algorithm can take to complete.
• For example:
• If we say certain algorithm takes 100 secs as longest amount of time. So, 100
secs will be Upper bound of that algorithm. The algorithm can take less than
100 secs but it will not take more than 100 secs.

1-10
10
Theta (Θ) Notation
• It is formal way to express the Upper bound and lower bound of an algorithm’s
running time.
• By lower and upper bound means for any give input this notation determines
average amount of time an algorithm can take to complete.
• For example:
• If we say certain algorithm takes 100 secs for first run, 120 secs for second run,
110 secs for third run and so on. So, theta notation gives an average of running
time of that algorithm.

1-11
11
Big –O complexity

1 Public int sum ( int x, int


2 y) {
3 int result=x + y;
4 return result;
}

Time complexity=O(1)

1-12
12
Big –O complexity (cont.)

1 Public int FindSum ( int


2 n) {
3 int sum=0;
4 for( int i=1; i<=n; i++)
5 sum=sum+i;
6 retern sum;
}

Time complexity=O(n)

1-13
13
Big –O complexity (cont.)

1 Public int FindSum ( int n) {


2 int sum=0;
3 for( int i=1; i<=n; i++){
4 for (int j=1;j<=n; j++){
5 sum=sum+i;}
6 }
return sum;
}

2
Time complexity=O(n )

1-14
14
Common Big –O complexities

1-15
15
Thank You

1-16
16
2/12/2024

You might also like