Introduction to the binary search algorithm
The binary search algorithm differs from the linear search algorithm in that the data structure, like an array, needs to be sorted to apply the algorithm. Binary search works by finding the middle value of the array and comparing it to the value that is being searched for. This narrows down the list of values so that every item in the array won’t need to be visited because they are higher or lower than the middle value. First, let’s set up the algorithm in pseudocode:
- Store the value being searched for.
- WHILE the value is not found.
- Take the array length and divide it by 2.
- Set the value as the midpoint index.
- IF the index value is less than the search value.
- Stop visiting values that are less than the midpoint index.
D. IF the index value is higher than the search value.
- Stop visiting values that are larger than the midpoint index.
E. IF the index value is equal to the search value.
- The value has been found, return the...