0% found this document useful (0 votes)
83 views

Analysis and Design of Algorithm (ADA) : Amity School of Engineering & Technology (CSE)

The document discusses the binary search algorithm, including its time complexity of O(log n), how it works by recursively dividing the search space in half, and examples of its implementation and use for searching a sorted array.

Uploaded by

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

Analysis and Design of Algorithm (ADA) : Amity School of Engineering & Technology (CSE)

The document discusses the binary search algorithm, including its time complexity of O(log n), how it works by recursively dividing the search space in half, and examples of its implementation and use for searching a sorted array.

Uploaded by

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

Amity School of Engineering & Technology (CSE)

Analysis and Design of Algorithm(ADA)

Introduction

Dr. Garima Aggarwal


Associate Professor-ASET

1
Amity School of Engineering & Technology (CSE)

2
Amity School of Engineering & Technology (CSE)

3
Amity School of Engineering & Technology (CSE)

4
Amity School of Engineering & Technology (CSE)

5
Amity School of Engineering & Technology (CSE)

6
Amity School of Engineering & Technology (CSE)

Learning Outcomes
Students will be able to

Design,
Analyze a
Apply Knowledge Implement and
problem and
of Mathematics, Understand and evaluate a
identify and
Science, Analyze the computer-based
define the
Engineering and recursive and system, process,
computing
computing non recursive component or
requirements
approaches to algorithms programmer to
appropriate to its
the problem. meet desired
solution.
results.

7
Amity School of Engineering & Technology (CSE)

Introduction
• Binary search is the most efficient searching algorithm.
• Binary search, also known as a half-interval search.
• For binary search, the array must be sorted in either ascending
or descending order (limitation).
• Binary search follows divide and conquer approach.
• It's time complexity of O(log2n) makes it very fast as
compared to other searching algorithms.

8
Amity School of Engineering & Technology (CSE)

Divide-and-Conquer Algorithm
• Step 1: If the problem size is small, solve this problem directly;
otherwise, split the original problem into 2 sub-problems with equal
sizes.

• Step 2: Recursively solve these 2 sub-problems by applying this


algorithm.

• Step 3: Merge the solutions of the 2 sub-problems into a solution of


the original problem.

5-9
Amity School of Engineering & Technology (CSE)

Algorithm Binary-Search
Binary Search(A[], min, max, d)
A : Array of Elements
min: Lowest Index of A
max: Highest index of A
d: key element
Step 1: (a) Repeat while min <=max
mid=(min+max)/2
(b) if d= A[mid]
Write “Successful Search”
Return mid.
© Else if d < A[mid]
max=mid-1
(d) else
min = mid +1
Step2: Return Null
Step 3: Exit

5 - 10
Amity School of Engineering & Technology (CSE)

Iteration Method
do until the pointers low and high meet each other.
mid = (low + high)/2
if (x == A[mid])
return mid
else if (x > A[mid]) // x is on the right side
low = mid + 1
else // x is on the left side high = mid - 1

11
Amity School of Engineering & Technology (CSE)

Binary_Search(A[], low, high, x) Recursive Method


// A[]: elements are in ascending order
// low, high: the bounds for searching in A[]
// x: the element to be searched
// If x = A[j], for some j, then return j else return –1
if (low > high) then return –1 // invalid range
if (low = high) then // if small Prob
if (x == ar[i]) then return i
else return -1
else // divide Problem into two smaller sub-problems
mid = (low + high) / 2
if (x == A[mid]) then return mid
else if (x < A[mid]) then
return Binary_Search(A[], low, mid-1, x)
else return Binary_Search(A[], mid+1, high, x)
Amity School of Engineering & Technology (CSE)

Example
Let us consider an array arr = {1, 5, 7, 8, 13, 19, 20, 23, 29}. Find the location
of the item 23 in the array [3].
1st Iteration
beg = 0   
end = 8  
mid = 4   
a[mid] = a[4] = 13 < 23, therefore  

II Iteration
• beg = mid +1 = 5   
• end = 8  
• mid = 13/2 = 6    
• a[mid] = a[6] = 20 < 23, therefore;  

13
Amity School of Engineering & Technology (CSE)
arr = {1, 5, 7, 8, 13, 19, 20, 23, 29}. To Find the item 23

III iteration
beg = mid + 1 = 7   
end = 8   
mid = 15/2 = 7  
a[mid] = a[7]   
 a[7] = 23 = item;   
therefore, set location = mid;   
The location of the item will be 7.   

14
Amity School of Engineering & Technology (CSE)

Time Complexity
Recurrence Relation for binary search algorithm:

5 - 15
Amity School of Engineering & Technology (CSE)

Time Complexity
Let we calculate the time complexity of binary search [4]:

T(n) = 
1 + T(n/2) = 
1 + (1 + T(n/4)) = 2 + T(n/4) = 
2 + (1 + T(n/8)) = 3 + T(n/8) = ...
k + T(n/2k) = ...
log n + T(n/2log n) = log n + T(1) = (*)
log n + 1 = Θ(log n).

16
Amity School of Engineering & Technology (CSE)

Test Your Skills…

• Consider a set of 13 elements in an array list the elements of array that


require largest number of key comparison when searched for binary search.
Find average number of comparisons made by binary search in successful
search and unsuccessful search in this array.

17
Amity School of Engineering & Technology (CSE)

Summary
• Learned binary search which searches in a sorted array by repeatedly
dividing the search interval in half.
• Binary search is an optimal searching algorithm with which we can
search the desired element efficiently.
• The idea of binary search is to use the information that the array is
sorted and reduce the time complexity to O(log2 n).
• Best Case: ɵ(1)
• Average Case: ɵ(log2n)
• Worst Case: ɵ(log2n)

18

You might also like