Your task is to implement a binary search algorithm.
A binary search algorithm finds an item in a list by repeatedly splitting it in half, only keeping the half which contains the item we're looking for. It allows us to quickly narrow down the possible locations of our item until we find it, or until we've eliminated all possible locations.
Binary search only works when a list has been sorted.
The algorithm looks like this:
Here's an example:
Let's say we're looking for the number 23 in the following sorted list: [4, 8, 12, 16, 23, 28, 32].
[23, 28, 32].[23].Your task is to implement a binary search algorithm.
A binary search algorithm finds an item in a list by repeatedly splitting it in half, only keeping the half which contains the item we're looking for. It allows us to quickly narrow down the possible locations of our item until we find it, or until we've eliminated all possible locations.
Binary search only works when a list has been sorted.
The algorithm looks like this:
Here's an example:
Let's say we're looking for the number 23 in the following sorted list: [4, 8, 12, 16, 23, 28, 32].
[23, 28, 32].[23].