Worked example 1
An integer array Data is sorted in ascending order: [3, 5, 8, 11, 15, 22, 29, 31]. Trace the execution of a binary search to find the value 22. The array has indices 0 to 7. Use Low, High, and (integer division).
Show solution outline
Target Value: 22 Initial state: ,
Pass 1:
- Data[Mid] is Data[3], which is 11.
- , so the target is in the upper half.
- New state: ,
Pass 2:
- Data[Mid] is Data[5], which is 22.
- . The value is found at index 5.
Result: Value 22 found at index 5.