Final answer:
The Binary Search algorithm efficiently finds a number in a sorted range by repeatedly halving the search space. Starting with boundary values, the process continues until the number is found, taking logarithmic time complexity to complete.
Step-by-step explanation:
The Binary Search algorithm is a highly efficient method used for finding an item in a sorted list. In our scenario, the computer is tasked to find a given integer 'n' within a specified range using binary search. The algorithm repeatedly divides the range in half, comparing the midpoint to the target number until it is found.
To implement binary search, we will need two initial boundary values: 'start' and 'end'. Here's a step-by-step approach for the guessing game:
- Set the 'start' to the lower end of the range and 'end' to the upper end of the range.
- Calculate the midpoint 'mid' as the average of 'start' and 'end'.
- If 'mid' is equal to 'n', the computer has found the number.
- If 'mid' is greater than 'n', adjust the 'end' to be 'mid - 1'.
- If 'mid' is less than 'n', adjust the 'start' to be 'mid + 1'.
- Repeat steps 2 through 5 until 'n' is found.
At each iteration, the range within which 'n' could lie is halved, significantly reducing the number of comparisons needed, making the binary search much faster than simple linear searches, especially with large datasets.