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 int binarySearch(const int list[], int key, int arraySize)
2 {
2 int low = 0;
3 int high = arraySize - 1;
4
5 while (high >= low)
6 {
6 int mid = (low + high) / 2;
7 if (key < list[mid])
8 high = mid - 1;
9 elseif (key == list[mid])
11 return mid;
12 else
13 low = mid + 1;
14 }
15
16 return -low - 1;// Now high < low
17 }