Binary Search - Divide and Conquer

Binary Search - Divide and Conquer

Binary search is an effective method that employs the divide and conquer strategy to quickly find an element in an ordered list, significantly reducing the search space by half at each step. This technique not only exemplifies algorithmic efficiency in solving search problems, but also highlights the importance of understanding fundamental computer science concepts, such as divide and conquer algorithms, which are crucial for optimizing and solving complex problems in a more intuitive way.

The goal of this exercise is to implement binary search in such a way that it returns the index of the searched element in the list (or -1 if it is not present) and the number of steps performed, providing a clear view of the divide and conquer process in action. In the image below, you can find an illustration of the algorithm at work. In case of even number of elements in the list, the left one is considered as the middle element.

example

Input: Two arguments. List of integers (int) and an integer.

Output: List of two integers.

Examples:

assert bin_search([1, 2, 3, 4, 5], 3) == [2, 1]
assert bin_search([], 2) == [-1, 1]
assert bin_search([1, 10, 100, 1000], 1000) == [3, 3]