Binary Search Algorithm

12
Binary Search Algorithm Submitted By: Akash Gupta 3 rd B.Tech Information Technology (243/07)

Transcript of Binary Search Algorithm

Page 1: Binary Search Algorithm

Binary Search Algorithm

Submitted By:Akash Gupta

3rd B.Tech Information Technology (243/07)

Page 2: Binary Search Algorithm

What basically a BSA is?

• A binary search is based on the divide and conquer strategy.

• A binary search is an algorithm for locating the position of an element in a sorted list by checking the middle element , eliminating half of the list from consideration, and then performing the search on the remaining half. If the middle element is equal to the searched value, then the position has been found; otherwise, the upper half or lower half is chosen for search based on whether the element is greater than or less than the middle element.

Page 3: Binary Search Algorithm

• Generally, to find a value in unsorted array, we should look through elements of an array one by one, until searched value is found. In case of searched value is absent from array, we go through all elements. In average, complexity of such an algorithm is proportional to the length of the array.

• Situation changes significantly, when array is sorted. If we know it, random access capability can be utilized very efficiently to find searched value quick. Cost of searching algorithm reduces to binary logarithm of the array length. For reference, log2(1 000 000) ≈ 20. It means, that in worst case, algorithm makes 20 steps to find a value in sorted array of a million elements or to say, that it doesn't present it the array.

• The method(BSA) reduces the number of elements needed to be checked by a factor of two each time, and finds the target value, if it exists in logarithmic time.

• I have said before that this algorithm is based on D&C strategy and the advantage of D&C strategy is that it reduces each problem to only one subproblem.

Page 4: Binary Search Algorithm

Algorithm:-Algorithm is quite simple. It can be done either recursively or iteratively:1) Get the middle element; 2) If the middle element equals to the searched value, the algorithm stops; 3) Otherwise, two cases are possible:

1.Searched value is less, than the middle element. In this case, go to the step 1 for the part of the array, before middle element. 2.Searched value is greater, than the middle element. In this case, go to the step 1 for the part of the array, after middle element.

Page 5: Binary Search Algorithm

Note: Now we should define, when iterations should stop. First case is when searched element is found. Second one is when subarray has no elements. In this case, we can conclude, that searched value doesn't present in the array

Case 1.When Element is there in the arrayList: Find 6 in {1, 5, 6, 18, 19, 25, 46, 78, 102, 114}.Step 1 (middle element is 19 > 6):

1 5 6 18 19 25 46 78 102 114

Step 2 (middle element is 5 < 6):

1 5 6 18 19 25 46 78 102 114

Step 3 (middle element is 6 == 6):

1 5 6 18 19 25 46 78 102 114

Page 6: Binary Search Algorithm

Case 2.When the element is not present in the list Find 103 in {1, 5, 6, 18, 19, 25, 46, 78, 102, 114}.

Step 1 (middle element is 19 < 103): 1 5 6 18 19 25 46 78 102 114 Step 2 (middle element is 78 < 103): 1 5 6 18 19 25 46 78 102 114 Step 3 (middle element is 102 < 103): 1 5 6 18 19 25 46 78 102 114Step 4 (middle element is 114 > 103): 1 5 6 18 19 25 46 78 102 114Step 5 (searched value is absent): 1 5 6 18 19 25 46 78 102 114

Page 7: Binary Search Algorithm

Coding Of The Algorithm:

int binarySearch(int arr[], int value, int left, int right) { while (left <= right) { int middle = (left + right) / 2; if (arr[middle] == value) return middle; else if (arr[middle] > value) right = middle - 1; else left = middle + 1; } return -1;}

Page 8: Binary Search Algorithm

Time Complexity Of Binary Search

• Suppose the time complexity is measured by the number ‘k’ of comparisons to locate the item in the arrayList.

• Observe that each comparison reduces the sample size in half.

• That is , which of the following will be first be<1? n/2,n/4,n/8……n/2k,….

Page 9: Binary Search Algorithm

• We solve the equation n/2k <1 , and get k>log2n

• So If we set k= log2n +1, then we know that after that many iterations , we will have found our item, or concluded that it was not there…

• It means, that algorithm will do at most log2(n) iterations, which is a very small number even for big arrays.

Page 10: Binary Search Algorithm
Page 11: Binary Search Algorithm

Limitations Of Binary Search Algorithm Since the binary search algorithm is very efficient , why would

one want to use any other search algorithm? Observe that BSA has two conditions:

(1)The list must be sorted…

(2)One must have direct access to the middle element in any sublist .This means that one must essentially use a sorted array to hold the data . But keeping data in a sorted array is normally very expensive when there are many insertions and deletions..

Page 12: Binary Search Algorithm

Graph For Binary Search Algorithm

0 20 40 60 80 100 120 1400

1

2

3

4

5

6

7

8

9

X-Values

Y-Values