- LinkList
Binary Search

Binary search is the most common algorithm in the search concept and how do binary search work? This is the main concept,Binary search works with lowerbound and upperbound, such as it is mandatory for the target array to be sorted.
after we will get middle,like image, If the middle value and the search value match We will return the position of the element, If the value does not match the lower value or the upper value is swap with the middle value, And loops are doing the same thing again,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pos=-1; | |
def binary_search(arr,n): | |
l=0; | |
r=len(arr); | |
while l<=r: | |
mid=(l+r)//2; | |
if arr[mid]==n: | |
globals()['pos']=mid; | |
return True; | |
else: | |
if arr[mid]<n: | |
l=mid; | |
else: | |
r=mid; | |
arr=[4,8,9,10,12,13]; | |
n=13; | |
if binary_search(arr,n): | |
print('element in ',pos+1); | |
else: | |
print('not found') |