DS Week 2 Basics of DS Lecture Algorithm Arrays TC Linear Binary Search by DR Gaurav
DS Week 2 Basics of DS Lecture Algorithm Arrays TC Linear Binary Search by DR Gaurav
Week 2 Lecture
by
Dr Gaurav Kumar
Asst. Prof, Bennett University
Topics
What is Data
Structure ??
DS - What, Why and How?
Structured
Unstructured
DS - What, Why and How?
Unstructured
Structured
Unstructured
DS - What, Why and How?
Unstructured
Structure is a Way of organizing information, so that it is easier to use.
DS – What ?
Unstructured
Operations on Data Structure
• Add an element
• Delete an element
• Traverse / Display
Unstructured
It provides reusability and abstraction .
DS – Why ?
D s I s A F u n d a m e n t a l C o n c e p t To C S
DS & Types
Types of Data Structure
4 bytes
Visualize Memory 1 Bytes 1 Bytes 1 Bytes 1 Bytes
Representation
2000 2001 2002 2003
Range of int- 0 up to 4,294,967,295 (232 - 1)
Types of Data Structure
20 Bytes
Non-Primitive Types
Non – Primitive Data Structure
For Example
Definition
Making a Delicious Tea
Step by step
procedures to solve
a problem
Input Output
Characteristics of Algorithm
• Input & Output: Zero or more input but at least one output
Algorithm Program
• A program is a set of instructions written in Programming
• An algorithm is a well-defined
Language (computer understandable language) to
sequence of steps (written in Natural
perform a certain task.
Language) to solve a given problem
• Program is an implementation of an algorithm
• Design Time
• Knowledge of Programming Language
• Domain Knowledge
• Testing
• Analyze
end function
Note: Pseudocode is not an actual programming language.
Role of Algorithm Designer
Can I do better?
Algorithm Designer
Analysis of Algorithms
• Given a particular problem of size n. The time required by any algorithm for solving, this problem
is denoted by a function such as f(n).
• Best-case − The minimum amount of time needed by the algorithm to complete a task
(best data or best input data)
• Average case − The average amount of time needed by the algorithm to complete a
task
Analysis of Algorithms- How?
Problem Statement
Searching a X Student in a Class
A1 A2
Algorithm 1 Algorithm 2
Problem Statement
Searching a X Student in a Class A1 A2
Algorithm 1 Algorithm 2
Method-1
Problem Statement
Searching a X Student in a Class
A1 A2
Algorithm 1 Algorithm 2
Problem
It might also be possible that for some
inputs, first algorithm perform better
on one machine and the second works
Machine 1 Machine 2
better on other machine.
Analysis of Algorithms – How?
✓ We determine the time and space complexity of an algorithm by just looking at the
algorithm rather than running it on a particular system with a different memory,
processor, and compiler. (Theoretical Analysis)
It doesn’t require algorithms to be implemented and the time taken by programs to be compared.
Different types of asymptotic notations are used to represent the complexity of an algorithm.
~ 35 Lakh
Price: 35 Lakh Price: 10,000/-
• Given a particular problem of size n. The time required by any algorithm for solving this
problem is denoted by a function such as f(n).
Question -1
F(n) = 1000 n2 + 5n3 + 6000n,
What is the Order of Function ?
a) f(n)= O(n2)
Which one is the
b) f(n)= O(6000n)
correct answer?
c) f(n)= O(n3)
d) I am confused
Correct Answer is c
Type and Comparison of Order of Function of Classes
(Valid for larger values of n)
O(1) < O(logn) < O(√n) < O(n) < O(nlogn) < O(n2) < O(n3) < O(2n) < O(10n) < O(nn)
Log2n n n2 n3 2n nn
n=1 0 1 1 1 2 1
n=2 1 2 4 8 4 4
n=4 2 4 16 64 16 256
n=8 3 8 64 512 256 1,67,77,216
n=5
Algorithm of Sum of n number Algorithm Sum (A, n)
A= 6 4 3 8 1
{ 0 1 2 3 4
S=0; 1
for ( i=0; i<n; i++) n+1 i=0 Sum instructions will be executed
}
i=4 Sum instructions will be executed
return S; 1 i<n
i=5 Sum instructions will not execute
}
Total= 3 words
Space Complexity= s(n)= constant = O(1)
*Assume that every variable takes one word space for storage
Understanding Space Complexity
Sum of n number
Correct Answer: C
General Assessment Time
10
9
8
7
6
5
4
3
2
1
What is the Space Complexity of this
algorithm?
A) O(1)
Algorithm print(n)
{ B) O(100)
for ( i=0; i<n; i++)
{ C) O(n)
printf(“This is number: %d”, i);
} D) None of the Above
}
Correct Answer: A
What is Array Data
Structure ??
DS- Array
• An array is a Linear type data structure used to
store a collection of data of same type.
double balance[10];
int arr[5];
base address
}
General Assessment Time
10
9
8
7
6
5
4
3
2
1
#include <iostream>
using namespace std; Which one is the
int main() correct answer?
{
a) 25
int a[] = {1,2,3,4};
b) 52
int b[4] = {5,6,7,8};
c) 36
Correct Answer is c cout<< a[2] << b[1];
d) 63
return 0;
}
Operations in Array
#include <iostream>
using namespace std;
int main(){
int num[] = {2,8,7,6,0};
int n = sizeof (num) / sizeof(num[0]);
for (int i = 0; i < n; i++)
Time Complexity
{
cout<< num[i]; Time Complexity for Traversal – O(n)
(in all cases)
}
return 0;
}
Insertion − Adding an element
int new_element = 9
2 8 7 6 0
int num[]={2,8,7,6,0};
int n = sizeof (num) / sizeof(num[0]);
n=n+1;
Beginning of the array
Insertion − Adding an element
2 8 7 6 0
2 8 7 6 0
Shifting last element to
the next available space
Beginning of the array
Insertion − Adding an element
int new_element = 9;
int num[]={2,8,7,6,0};
int n = sizeof (num) / sizeof(num[0]);
2 8 7 6 0
n=n+1;
0 1 2 3 4 5
num[5]=num[4];
num[0] num[1] num[2] num[3] num[4] num[5]
num[4]=num[3];
num[3]=num[2];
num[2]=num[1];
num[1]=num[0];
num[0]=new_element;
9 2 8 7 6 0
Insertion − Adding an element
Case 2 : Add the new element at the
beginning of the array
int new_element = 9; 2 8 7 6 0
int num[]={2,8,7,6,0}; num[0] num[1] num[2] num[3] num[4] num[5]
int n = sizeof (num) / sizeof(num[0]);
n=n+1;
for (i = n-1; i >=1; i--) Time Complexity
num[5]=num[4];
num[4]=num[3]; { Time Complexity– O(n)
num[3]=num[2]; num[i] = num[i-1];
num[2]=num[1];
}
num[1]=num[0];
num[0]=new_element;
num[i]=new_element;
Insertion − Adding an element
int num[n]={2,8,7,6,0};
num[5]=num[4]; At this index of the array
num[4]=num[3];
num[3]=num[2]; Time Complexity
num[2]=new_element; Time Complexity– O(n)
Deletion of an element in Array
8 7 6 0
num[0] num[1] num[2] num[3] num[4]
2 8 5 6 0
num[0] num[1] num[2] num[3] num[4]
string letters[2][4]; {
{ "A", "B" },
string letters[2][4] = {
{ "C", "D" }
{ "A", "B", "C", "D" },
},
{ "E", "F", "G", "H" }
{
};
{ "E", "F" },
{ "G", "H" }
}
};
Initializing 2-D Arrays
1D Array
Suppose an array, A[-10 ..... +2 ] having Base address (BA) = 999 and size of an element = 2
bytes, find the location of A[-1].
2D Array
Note: 2-D arrays exists only from the user point of view and created to implement a relational
database table look alike data structure. In computer memory, the storage technique for 2D
array is similar to that of an one dimensional array.
• All the rows of the 2D array are stored into the memory contiguously.
• First, the 1st row of the array is stored into the memory completely, then
the 2nd row of the array is stored into the memory completely and so on
till the last row.
Address(a[i][j]) = B. A. + (i * n + j) * size
or
Address(a[i][j]) = B. A. + ((i -lr)* n + (j-lc)) * size
lr - Lower limit of row or start limit of the row or assume 0 if it is not given
lc- lower limit of column o start limit of column or assume 0 if it is not given.
Accessing Element of in 2D Array
Address(a[i][j]) = B. A. + ((i-lr) * n + (j-lc)) * size
Example-
Given an array, arr[1………10][1………15] with base value 100 and the size of each element
is 1 Byte in memory. Find the address of arr[8][6] with the help of row-major order.
01 Linear Search
02 Binary Search
Visualization of Linear Search
8 3 1 2 4 5 6 7
Algorithm of linear search
Key Element = 4 Step 1- Start from the leftmost element of arr[] and one
Key Element = 7
Step 3- If x doesn’t match with any of the elements,
Total Number of Comparison = 8 return -1.
Visualization of Linear Search
8 3 1 2 4 5 6 7
Key Element = 8
Total Number of Comparison = 1
f(n) = 1 = Constant = O(1)
8 3 1 2 4 5 6 7
Key Element = 7
Total Number of Comparison = 7
8 3 1 2 4 5 6 7
Key Element = 8 or 3 or 1 or 2 or 4 or 5…
Total Avg Time = All possible case time divided by number of cases
Average Case Time for n elements Average Time= (1+2+3+...n)/n = n(n+1)/2n = n+1/2
Complexity Avg Case Time Complexity
f(n) = O(n)
Visualization of Searching Algorithms
2. Binary Search
• Binary search is a fast way to find an item in a sorted list.
• It follows the divide and conquer approach in which the list is divided into
two halves, and the item is compared with the middle element of the list.
• If the match is found then, the location of the middle element is returned.
Otherwise, we search into either of the halves depending upon the result
produced through the match.
Visualization of Searching Algorithms
2. Binary Search
Visualization of Binary Searching Algorithms
1. Divide the search space into two halves by finding the middle index
“mid”.
2. Compare the middle element of the search space with the key.
4. If the key is not found at middle element, choose which half will be used as the next search space.
1. If the key is smaller than the middle element, then the left side is used for next search.
2. If the key is larger than the middle element, then the right side is used for next search.
5. This process is continued until the key is found or the total search space is exhausted.
Time Complexity of Binary Searching Algorithms
next search. {
low = mid + 1;
Step 5. This process is continued until the key is found or the total search space is }
}
exhausted.
return -1;
}
Next Week Topic for Prior Readings
Recursion