A simple console game implementation using binary search algorithm and GO.
In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array.
Iteration 1
- low = 0
- high = 16
- middle = (0+16)/2 = 16/2 = 8
- 7 (target at index 4) < 14 (middle at index 8)
- high = middle - 1 = 7
Iteration 2
- low = 0
- high = 7
- middle = (0+7)/2 = 7/2 = 3 R 1
- 7 (target at index 4) > 6 (middle at index 3)
- low = middle + 1 = 4
Iteration 3
- low = 4
- high = 7
- middle = (4+7)/2 = 11/2 = 5 R 1
- 7 (target at index 4) < 8 (middle at index 5)
- high = middle - 1 = 4
Iteration 4
- low = 4
- high = 4
- middle = (4+4)/2 = 8/2 = 4
- 7 (target at index 4) == 7 (middle at index 4)