HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
Binary.pptx
1. Binary Search
☐ Name : A.S.M Sabbir Ahmed
☐ Roll No. :2110676154
☐ Semester : 4th
☐ Year : 2024
2. 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.
☐
3. 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.
4. 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
6. 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)
7. Example
Iteration 1:
m=2
low=3 //here key=9 is greater than A[m]=7 ,thus it
executes last else condition .
Since,(3<4) loop will continue.
Iteration 2:
m=3
return 3;//Since key=9 is equal to A[m]=9 , where m=3
, it returns 3 and thus ends its binary search
here by returning the index where the key
value is stored.
8. 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.
☐
9. 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).
☐
10. 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.
11. 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.
12. 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.
13. 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