Binary Search

Run Settings
LanguagePython
Language Version
Run Command
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] def binary(num, seq): min = 0 max = len(seq) - 1 step = 0 while True: if max < min: return -1 mid = (min + max) // 2 step += 1 print("step {0}: checking element {1}".format(step, mid + 1)) if seq[mid] < num: min = mid + 1 elif seq[mid] > num: max = mid - 1 else: print("found it!") return binary(3, a)
Editor Settings
Theme
Key bindings
Full width
Lines