Basic idea behind binary search is to divide sorted array in two parts, and check whether the element which divides the array is desired element . And if the desired element is found then stop the search, otherwise further
divide the array until the desired element is not found.
Step-Wise Pictorial Demonstration of Search Procedure
Binary Search in Python
To demonstrate binary search in Python. I have taken a list of integers a of having 10 elements. We have set item 18 to be searched.
Furthermore, see the program and you can easily understand if you have understood pictorial demonstration of search procedure.
while start <= end and item != a[mid]:
if item == a[mid]:
print(‘Position of the given element is’,mid+1)
if item < a[mid]:
Time Complexity of Binary Search Algorithm
Time complexity of binary search algorithm is O(log2n). However, it requires a sorted array to perform search operation.