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

Binary

Binary search is a search algorithm that finds the position of a target value within a sorted array. It works by repeatedly dividing the search interval in half. Each iteration eliminates half of the elements from consideration. This continues until the value is found or the search interval is empty. Binary search runs in logarithmic time O(log n) and uses constant O(1) space, making it an efficient algorithm for searching sorted data.

Uploaded by

Tahamid Hasan.
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)
7 views

Binary

Binary search is a search algorithm that finds the position of a target value within a sorted array. It works by repeatedly dividing the search interval in half. Each iteration eliminates half of the elements from consideration. This continues until the value is found or the search interval is empty. Binary search runs in logarithmic time O(log n) and uses constant O(1) space, making it an efficient algorithm for searching sorted data.

Uploaded by

Tahamid Hasan.
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/ 13

Binary Search

☐ Name : A.S.M Sabbir Ahmed


☐ Roll No. :2110676154
☐ Semester : 4th
☐ Year : 2024
Introduction

☐ The Binary Search can be implemented on only


sorted list of elements.
☐ The process starts with finding the middle of interval and
comparing the key with it (mid point), if the value of the
key is less than it then the search interval reduces up to mid
point and if the value of the key is greater than it then we
search the key from mid point to the end of interval.
Introduction

☐ Binary search, also known as half-interval search or


logarithmic search, is a search algorithm that finds
the position of a target value within a sorted array.

☐ Binary search runs in at worst logarithmic time ,


making {O(log n)} comparisons, where {n} is the
number of elements in the array and {log } is the
binary logarithm ; and using only
constant {(O(1))}space.
Algorithm
Binary Search ( A[0 1 2 3…….n-1] , key)
low 0
high n-1 while(low
<= high) do
{
m(low + high)/2 if
(key=A[m]) then
Algorithm
return m
else if (key<A[m])
then
high m-1
else
low m+1
}
Example
☐ Let the number givens be 4,6,7,9,10 and the key is 9.
☐ So , according to the algorithm these numbers are to be
stored in a 1D array named A[0 1 2….n-1];where n=
number of elements i.e. in this example 5.
☐ Thus according to the algorithm A[0 1 2 3 4] is generated
and key value is passed in the array that is 9.

Binary Search( A[0 1 2 3 4] , 9 )


low=0
high=4
while(0<=4)
Example
Iteration 1:
m=2
low=3
//here
key=9 is
greater
than
A[m]=7 ,
thus it
e
x
e
c
Use Case
☐ Suppose we have an array A and in this array we are
searching for a value K. If A has no special properties, then
there is no better way to search K than linear search -- to
start at the beginning and go through the array one step at a
time, comparing each element to K in turn. The time it takes
(on average, and in the worst case) is linear, or O(N), to the
number of items of array.
But if A is a sorted array, there is a much faster way,
☐ Binary Search, to look for K. In binary search, after each
iteration, the length of the array we are looking in gets cut
in half.
Space Analysis
☐ Binary Search can be done using recursion and Iteration .

If we continuously divide the interval into two halves then


☐ we can do it at most log(n) time . So we can say the number
of comparision is at most log(n).So the number of time
recursion will be done is log(n).So,recursive space
complexity is log(n).But In case of iteration we need only
three variables high, mid and low.So in that case complexity
is ~O(1).
Time Analysis
☐ Best case - O (1) comparisons : In the best case, the item X
is the middle in the array A. A constant number of
comparisons (actually just 1) are required.

☐ Worst case - O (log n) comparisons : In the worst case, the


item X does not exist in the array A at all. Through each
comparision of Binary Search, the size of the admissible
range is halved. This halving can be done ceiling(log n )
times. Thus, ceiling(log n ) comparisons are required.
Time Analysis
☐ Average case - O (log n) comparisons : To find the average
case, take the sum over all elements of the product of
number of comparisons required to find each element and
the probability of searching for that element. To simplify
the analysis, assume that no item which is not in A will be
searched for, and that the probabilities of searching for each
element are uniform.
Advantage and Disadvantage
☐ Advantage:
1. Binary search is an optimal searching algorithm using
which we can search desired element very efficiently.

☐ Disadvantage:
1. This algorithm requires the list to be sorted . Then only
this method is applicable.
Application

1. The binary search is an efficient searching method and is


used to search desired record from database.

2. For solving nonlinear equations with one unknown this


method is used.

Thank-You

You might also like