Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Module 2A Binary Search

Uploaded by

Preeti Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Module 2A Binary Search

Uploaded by

Preeti Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Amity School of Engineering and Technology

Amity School of Engineering & Technology


B.Tech CSE , 5th Semester

Analysis & Design of Algorithms


Dr. Garima Aggarwal
Module Objectives
Discuss Applications of Divide and Conquer Approach

2
Learning Outcomes
Students will be able to

3
Contents

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

5
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-6
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-7
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

8
Recursive Method
Binary_Search(A[], low, high, x)
// 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)
Example

10
arr = {1, 5, 7, 8, 13, 19, 20, 23, 29}. To Find the item 23

11
Time Complexity
Recurrence Relation for binary search algorithm:

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

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

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

15

You might also like