Roadmap to Membership of RICS - Pathways and Routes
Binary Search
1. Binary Search
Name : Kunj Desai
Enrollment No. :140950107022
Semester : 5th
Year : 2016
2. Introduction
The Binary Search can be implemented on only
sorted list of elements.
The process starts with finding the element at the
middle of the list and comparing the key value with the
element in the middle of the list, if the value of the key
is less than the element at the middle then the search
process the key to the list up to the middle element
and if the value of the key is greater than element at
the middle then search the key from middle element to
the end of the list.
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. Time Analysis
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. Time Analysis
Binary Search can be analyzed with the best, worst,
and average case number of comparisons. These
analyses are dependent upon the length of the array,
so let N =|A| denote the length of the Array A.
The numbers of comparisons for the recursive and
iterative versions of Binary Search are the same, if
comparison counting is relaxed slightly. For Recursive
Binary Search, count each pass through the if-then-
else block as one comparison. For Iterative Binary
Search, count each pass through the while block as
one comparison.
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 recursion or iteration 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