A = [-1, 2, 3, 5, 6, 8, 9, 10]key = 7 l = 0r = len(A) - 1 while l < r-1: mid = l + (r-l)//2 print(l, r, mid) if A[mid] <= key: l = mid else: r = mid print(mid, A[mid])
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 -1
0 7 3 3 7 5 3 5 4 4 6
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!