0% found this document useful (0 votes)
13 views83 pages

DS Week 2 Basics of DS Lecture Algorithm Arrays TC Linear Binary Search by DR Gaurav

Uploaded by

titirshaasingh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views83 pages

DS Week 2 Basics of DS Lecture Algorithm Arrays TC Linear Binary Search by DR Gaurav

Uploaded by

titirshaasingh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 83

Subject: Data Structure

Using C++ (CSET202)

Week 2 Lecture

Introduction to DS, Algorithms, Time


Complexity, Array, and Searching
What, Why and How?

by

Dr Gaurav Kumar
Asst. Prof, Bennett University
Topics

• DS – What, Why and How ?

• Types of Data Structure

• Algorithms, Time Complexity: Asymptotic Analysis,


Big-Oh Notation

• Handling Arrays, Insertion, Deletion, Traversal, Linear


Search, Binary Search
DS - What, Why and How?

What is Data
Structure ??
DS - What, Why and How?

Data can be defined as numbers,


images, videos, concepts, or
instructions which should be suitable
for communication, interpretation, or
processing by human or electronic
machine.
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 ?

A data structure is a particular way of


organizing data in a computer so that it
can be used efficiently.

Unstructured
Operations on Data Structure

• Search for a data element

• Add an element

• Delete an element

• Traverse / Display

• Sort the list of elements


DS - Why?

To solve the complex requirements in efficient way we need this study.

Provide fastest solution of human requirements.

Necessary for designing efficient algorithms.

Manipulation of large amounts of data is easier.

Appropriate data structure can help programmers save a good amount of


Time and Space

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

Primitive Data Structure Fundamental


int a;
type of data structure that stores the
single data of only one type. a a=5;
Memory
Ex: int, char, double etc.
5 Representation

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

Non-Primitive Data Structure User defined


Arrays data structure that stores the multiple data
values in a single entity.
int a[5];
Ex- Arrays, Linked Lists, Stack, Queues,
Trees, Graphs

a[0] a[1] a[2] a[3] a[4]

20 Bytes
Non-Primitive Types
Non – Primitive Data Structure

Linear Data Structures: Traverses the


data elements sequentially, in which
only one data element can directly be
reached.
Ex: Array, Linked List.
Non – Primitive Data Structure

Non-Linear Data Structures: Every data item is


attached to several other data items in a way that is
specific for reflecting relationships (Hierarchical
Manner). The data items are not arranged in a sequential
structure. Ex: Trees, Graphs
Algorithm

For Example
Definition
Making a Delicious Tea
Step by step
procedures to solve
a problem

Input Output
Characteristics of Algorithm

• Effectiveness: Each steps must be feasible and essential to


the requirements

• Definiteness: Each instruction must be clear, well defined


and unambiguous

• Input & Output: Zero or more input but at least one output

• Finiteness: Finite number of steps and must terminate within


a finite amount of time
Why Algorithm

Once we find an algorithm for All the knowledge required for


solving a problem, we do not need solving the problem is present in
to re-discover it the next time we the algorithm.
are faced with that problem.

Makes it easy when explaining


the process to others
How Algorithm is different from Program

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

• Independent from OS and H/W • Dependent on OS and H/W


How Algorithm

Example 5, 8, 2, 32, 12, 9, 11, 3


Algorithm of linear search : Program for Linear Search :

Step 1: Start from the leftmost element of


int search(int arr[], int n, int x)
arr[] and one by one compare x with each
{
element of arr[].
int i;
Step 2: If x matches with an element, return for (i = 0; i < n; i++)
the index.
if (arr[i] == x)
Step 3: If x doesn’t match with any of return i;
elements, return -1. return -1;
}
How Algorithm (Pseudo Code)

Example 5, 8, 2, 32, 12, 9, 11, 3


• It is one of the mathematical notation to represent an algorithm for a
Pseudocode for linear search :
program.
function linearsearch(list, searchterm):
• It does not have a specific syntax like any of the programming
for index from 0 -> length(list):
languages and thus cannot be executed on a computer.
if list[index] == searchterm then
• It is easier to read and understood by programmers who are familiar
return index
with different programming languages.
endif
• Pseudocode allows you to include several control structures such as
endloop
While, If-then-else, Repeat-until, for and case, which is present in
return -1 many high-level languages.

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).

Time complexity defines the total amount of time an algorithm needs to


execute all its key statements and in generating the output. (running time)

Space Complexity: Amount to memory needed by the algorithm for its


completion.

Good Algorithm: Solve a problem in less amount of time or space


complexity or both.
Analysis of Algorithms
Problem Statement: Finding a number X from a given list of
numbers.

Example 5, 8, 2, 32, 12, 9, 11, 3

For n input size, we perform the three types of analysis −

• Worst-case − The maximum amount of time needed by the algorithm to complete a


task (worst data or worst input size)

• 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

Given two algorithms for the above task,


how do we find out which one is better ?
Analysis of Algorithms – How?

Problem Statement
Searching a X Student in a Class A1 A2
Algorithm 1 Algorithm 2

Method-1

Implement both the algorithms (run the two


programs) on your computer for some inputs
and see which one takes less time.
Analysis of Algorithm – How?

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?

Apriori and Apostiari Analysis

• Apriori analysis means analysis is performed prior to running it on a specific system.

✓ 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)

• Apostiari analysis of an algorithm means we perform analysis of an algorithm only


after running it on a system. It directly depends on the system and changes from system
to system. (Empirical Analysis)
Asymptotic Notation
The main idea of asymptotic analysis is to have a measure of the complexity of algorithms that don’t depend on
machine-specific constants.

It doesn’t require algorithms to be implemented and the time taken by programs to be compared.

It is used for very large value of n (data sets)

Different types of asymptotic notations are used to represent the complexity of an algorithm.

O − Big Oh (Tightly Upper Bound)

Ω − Big omega (Tightly Lower Bound)

θ − Big theta (both Lower and Upper Bound)

o − Little Oh (Strictly Upper Bound)

ω − Little omega (Strictly Lower Bound)


Example of Asymptotic Analysis
(Best suitable for very large value of n or data sets)

~ 35 Lakh
Price: 35 Lakh Price: 10,000/-

• The simplest example is a function ƒ (n) = n2 + 3n, the term 3n


becomes insignificant compared to n2 when n is very large.

• The function "ƒ (n) is said to be asymptotically equivalent to n2 as


n → ∞", and here is written symbolically as ƒ (n) ~ n2.
O − Big Oh Notation

• Tightly Upper Bound of an Algorithm

• 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).

Let's assume, f(n) and g(n) are two functions

f(n) = O(g(n)) if there exists a positive integer n 0

and a positive constant c, such that


f(n) ≤ c.g(n) ∀ n≥n 0
General Assessment Time
10
9
8
7
6
5
4
3
2
1

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=10 3.2 10 100 1000 1024 1010


Analyzing Time Complexity

Time Complexity: Time complexity defines the total amount of


time an algorithm needs to execute all its key statements and in
generating the output. (running time)

Algorithm of Swapping Two Numbers

Algorithm Swap (a, b)


{
temp=a; 1 unit of time
a=b; 1 unit of time
b=temp; 1 unit of time
}
Total= 3 unit of time = O(1) = constant
*Assume that every statement take one unit of time for execution
Analyzing Time Complexity
Time Complexity: Time complexity defines the total amount of time an algorithm
needs to execute all its key statements and in generating the output. (running time)

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=0 -> 1 i=1


{
Sum instructions will be executed
i++ -> n
i=2 Sum instructions will be executed
S= S + A[i]; i<n -> n+1
n i=3 Sum instructions will be executed

}
i=4 Sum instructions will be executed

return S; 1 i<n
i=5 Sum instructions will not execute

} condition checked 6 times


f(n) = 2n+3 i++, i has changed 5 times
*Assume that every statement take one unit of time for execution = O(n)
Understanding Space Complexity
Algorithm of Swapping Two Numbers

Algorithm Swap (a, b) Total Variable


{ a 1 word
temp=a;
a=b;
b 1 word

b=temp; temp 1 word

}
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

Algorithm Sum (A, n) Space Complexity


{
S=0; A ----->n
for ( i=0; i<n; i++) n------>1
{
S------>1
S= S + A[i];
i------>1
}
return S; S(n) = n+3 = O(n)
}
General Assessment Time
10
9
8
7
6
5
4
3
2
1
What is the Time 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: 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.

• All arrays consist of contiguous memory


locations.

Syntax type arrayName [arraySize];

• Element − Each item stored in an array is called an element.

• Index − Each location of an element in an array has a numerical


index, which is used to identify the element.
Array Initialization

double balance[10];

Type Array size


name

double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};


Array Visualization in Memory

int arr[5];

base address

Meaning of contiguous memory allocations


Array Program
Storing the arrays values Printing the arrays value
#include <iostream>
using namespace std; for (i = 0; i < 5; i++)

int main() { cout<<var[i];


{ }
int var[5], i;
for (i = 0; i < 5; i++)
return 0;
{
cin>>var[i]; }

}
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

• Traverse − print all the array elements one by one.

• Insertion − Adds an element at the given index.

• Deletion − Deletes an element at the given index.

• Update − Updates an element at the given index.

• Search − Searches an element using the given index or by the value.

• Sorting – Arrange the elements in ascending or descending order


Traversing (printing) Array Elements

#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

Add an Element 9 #include <iostream>


using namespace std;
int main(){
int num[] = {2,8,7,6,0};
int n = sizeof (num) / sizeof(num[0]);
No Space in the Existing Array, for (int i = 0; i < n; i++)

We can not add any new element. {


cout<< num[i];
}
return 0;
}
Insertion − Adding an element

Create New Space before 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

New Element can be added

1. At the end of the array

2. At the beginning or at any index


Insertion − Adding an element

Case 1 : Add the new element at the end of the array


#include <iostream>
using namespace std;
int main(){
int new_element = 9;
int num[] = {2,8,7,6,0};
int n = sizeof (num) / sizeof(num[0]);
2 8 7 6 0 9
n=n+1;
num[n-1]=new_element;
for (int i = 0; i < n; i++){
Time Complexity
cout<< num[i]; Time Complexity of Insertion at the
} end of array – O(1)
return 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};
int n = sizeof (num) / sizeof(num[0]);
n=n+1;
Beginning of the array
Insertion − Adding an element

Case 2 : Add the new element at the beginning of the array

Shifting element to the next available space

2 8 7 6 0

Beginning of the array


Insertion − Adding an element

Case 2 : Add the new element at the beginning of the array

2 8 7 6 0
Shifting last element to
the next available space
Beginning of the array
Insertion − Adding an element

Case 2 : Add the new element at the beginning of the array

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

Case 3 : Add the new element at some index of an array

int new_element = 12;


int index=2;
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;

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

Element can be deleted

1. At the end of the array

2. At the beginning or at any index


Deletion of an element in Array

Case 1 : Element can be deleted at the end of the array

Last element of the array

Just reduce the one size of the array


Time Complexity
Time Complexity of Deletion– O(1)
Deletion of an element in Array

Case 2 : Element can be deleted at the any Index of the array

8 7 6 0
num[0] num[1] num[2] num[3] num[4]

num []= {2,8,7,6,0};


int n = sizeof (num) / sizeof(num[0]);
Element at Index 0 of the array num[0]=num[1];
num[1]=num[2];
num[2]=num[3];

Time Complexity num[3]=num[4]; 8 7 6 0


n=n-1;

Time Complexity of Deletion– O(n)


General Assessment Time
10
9
8
7
6
5
4
3
2
1

Array in C++ indicates?

A) A group of elements of same data type.

B) An array contains more than one element

C) Array elements are stored in memory in continuous or


contiguous locations.

Answer: D D) All the above.


General Assessment Time
10
9
8
7
6
5
4
3
2
1

Choose a correct statement about arrays ?

A) An array size can not change once it is created.

B) Array element value can be changed any number of times

C) To access Nth element of an array students, use students[n-1]


as the starting index is 0.

D) All the above


Answer: D
Updating an element in Array

2 8 5 6 0
num[0] num[1] num[2] num[3] num[4]

int num []= {2,8,7,6,0};


num[2]=5;
Update Value
with 5 for (int i = 0; i < 5; i++)
{
cout<<num[i];
Time Complexity
}
Time Complexity for updation– O(1)
Multi-Dimensional Arrays
A multi-dimensional array is an array of arrays. string letters[2][2][2] = {

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

for ( int i=0; i<n ;i++)


{
for (int j=0; j<n; j++)
{
a[i][j] = 0;
}
}
Accessing Element of an Array

1D Array

Address of element A[i] = base address + size * ( i - first index)

Suppose an array, A[-10 ..... +2 ] having Base address (BA) = 999 and size of an element = 2
bytes, find the location of A[-1].

Address of (A[-1]) = 999 + 2 x [(-1) - (-10)]


= 999 + 18 =1017
Accessing Element of in 2D Array

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.

Visualization/Representation of 2-D arrays in Memory

• Row Major ordering

• Column Major Ordering


Accessing Element of in 2D Array
Visualization/Representation of 2-D arrays in Memory

• Row Major ordering

• 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.

a11 a12 a13 a21 a22 a23 a31 a32 a33


Accessing Element in 2D Array
Visualization/Representation of 2-D arrays in Memory

• Row Major ordering

If array is declared by a[m][n] where m is the number of rows while n


is the number of columns, then address of an element a[i][j] of the
array stored in row major order is calculated as,

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.

Row Subset of an element whose address to be found i = 8


Column Subset of an element whose address to be found j = 6
Address of A[8][6] = 100 + 1 * ((8 – 1) * 15 + (6 – 1))
Lower Limit of row/start row index of matrix lr = 1
= 100 + 1 * ((7) * 15 + (5))
Lower Limit of column/start column index of matrix lc = 1
= 100 + 1 * (110)
Number of column given in the matrix (n)
Address of a[i][j] = 210
n = Upper Bound – Lower Bound + 1
= 15 – 1 + 1
= 15
Accessing Element in 2D Array
Visualization/Representation of 2-D arrays in Memory

• Column Major ordering


If array is declared by a[m][n] where m is the number of rows
while n is the number of columns, then address of an element
a[i][j] of the array stored in column major order is calculated as,

Address(a[i][j]) = B. A. + (i + j*m) * size


or
Address(a[i][j]) = B. A. + ((i -lr)+ (j-lc)*m) * 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.
Visualization of Searching Algorithms

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

Total Number of Comparison = 5 by one compare x with each element of arr[].

Step 2- If x matches with an element, return the index.

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

Time Complexity Analysis

Searching the key element present at the first index of a list

Key Element = 8
Total Number of Comparison = 1
f(n) = 1 = Constant = O(1)

Best Case Time Complexity


Visualization of Linear Search

8 3 1 2 4 5 6 7

Time Complexity Analysis

Searching the key element present at the last index of a list

Key Element = 7
Total Number of Comparison = 7

for n elements f(n) = n

Worst Case Time Complexity


f(n) = O(n)
Visualization of Linear Search

8 3 1 2 4 5 6 7

Time Complexity Analysis


Searching the key element present at any random index of a list

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.

3. If the key is found at middle element, the process is terminated.

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

(Iterative Method Approach) int binarySearch(int[] A, int x)


{
int low = 0, high = A.length - 1;
Step 1. Divide the search space into two halves by finding the middle index “mid”. while (low <= high)
{
Step 2. Compare the middle element of the search space with the key.
int mid = (low + high) / 2;
Step 3. If the key is found at middle element, the process is terminated. if (x == A[mid])
{
Step 4. If the key is not found at middle element, choose which half will be used as the
return mid;
next search space. }
TC= O(Logn)
else if (x < A[mid])
Step 4.1 If the key is smaller than the middle element, then the left side is used for
{
How ??
next search. high = mid - 1;
}
Step 4.2 If the key is larger than the middle element, then the right side is used for else

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

Structure, Pointers and Dynamic Memory


Allocation

Recursion

Recurrence Relations, Binary Search


using Recursion and Time Complexity

Tower of Hanoi Problems


Any Queries?
Office: MCub311
Email: [email protected]

You might also like