Binary search algorithm
We must deal with an ordered list.
For example, find the following list of 23 of the index.
[1,2,3,4,5,6,7,8,9,23,34,45,56,87,94]
Code:
l = [1,2,3,4,5,6,7,8,9,23,34,45,56,87,94] def find(l,aim,start= 0,end = len(l)): mid_index = (end - start) // 2 +start if l[mid_index] < aim: find(l, aim, start=mid_index + 1, end=end) elif l[mid_index] > aim: find(l, aim, start=0, end=mid_index - 1) else: print(mid_index,aim) find(l,23)