Binary Search

13
Binary Search Name : Kunj Desai Enrollment No. :140950107022 Semester : 5 th Year : 2016

Transcript of Binary Search

Page 1: Binary Search

Binary Search Name : Kunj Desai Enrollment No. :140950107022 Semester : 5th Year : 2016

Page 2: Binary Search

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.

Page 3: Binary Search

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.

Page 4: Binary Search

AlgorithmBinary Search ( A[0 1 2 3…….n-1] , key)

low 0high n-1while(low <= high)do{m(low + high)/2if (key=A[m])then

Page 5: Binary Search

Algorithmreturn melse if (key<A[m])then

high m-1else

low m+1}

Page 6: Binary Search

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=0high=4while(0<=4)

Page 7: Binary Search

ExampleIteration 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=3return 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.

Page 8: Binary Search

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.

Page 9: Binary Search

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.

Page 10: Binary Search

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.

Page 11: Binary Search

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.

Page 12: Binary Search

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.

Page 13: Binary Search

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