Binary Search Algorithm Submitted By: Akash Gupta 3rd B.Tech Information Technology (243/07)
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
• • 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.
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.
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
46
78
5
6 102
18
19 114
25
Step 2 ( middle element is 5 < 6 ): 46
1
78
5
6 102
18 114
19
Step 3 ( middle element is 6 == 6 ):
25
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
1
5
6
18 19
1
5
6
18 19
25 46 78 102 114 Step 2 (middle element is 78 < 103): 25 46 78 102 114 Step 3 (middle element is 102 < 103): 25 46 78 102 114 Step 4 (middle element is 114 > 103): 1 5 6 25 46 78 102 114
18
19
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; }
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,….
• 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.
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..
Graph For Binary Search Algorithm