Binary SearchBinary search is the search technique which works efficiently on the sorted lists. Hence, in order to search an element into some list by using binary search technique, we must ensure that the list is sorted. Binary search follows divide and conquer approach in which, the list is divided into two halves and the item is compared with the middle element of the list. If the match is found then, the location of middle element is returned otherwise, we search into either of the halves depending upon the result produced through the match. Binary search algorithm is given below. BINARY_SEARCH(A, lower_bound, upper_bound, VAL)
Complexity
ExampleLet us consider an array arr = {1, 5, 7, 8, 13, 19, 20, 23, 29}. Find the location of the item 23 in the array. In 1st step : in Second step: in third step: ![]() Binary Search Program using RecursionC programOutput: Enter the item which you want to search 19 Item found at location 2 JavaOutput: Enter the item which you want to search 45 the location of the item is 5 C#Output: Enter the item which you want to search 20 Item found at location 3 PythonOutput: Enter the item which you want to search ? 96 Item found at location 9 Enter the item which you want to search ? 101 Item not found Binary Search function using Iteration
Next TopicBubble Sort
|