Usage: Perform a binary search. Enter a key as a number. Click the Next button to perform one comparison. Click the Reset button to start over with a new
random list of the specified size (min 3 and max 20). The Custom Input button enables you to enter a custom list.
low: 0
↓
mid: 1
↓
high: 1
↓
low
high
mid
A list is filled with random numbers.
1 def binarySearch(lst, key):
2 low = 0
3 high = len(lst) - 1
4
5 while high >= low:
6 mid = (low + high) / 2
7 if key < lst[mid]:
8 high = mid - 1
9 elif key == lst[mid]:
10 return mid
11 else:
12 low = mid + 1
13
14 return -low - 1# Now high < low