Welcome to part three of the collection for Computer Science Algorithms. Once more we'll be delving into recursion by covering the topic of Binary Searches.
A "Binary Search" is a search algorithm that is used on an already sorted array. It compares the target value to the middle element of an array. If they don't match, the half in which the target cannot lie is ignored and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the target value is not contained in the array eventually the left search index and the right search index will cross and that condition should terminate the search.
For the sake of simplicity I'll refer to the array as "arr", the beginning index as "left", the end index as "right", and the element that we're searching for as "elem". The input for left and right initially will be left = 0 and right = sizeOfArray - 1. The rest of the algorithm can be broken down in five steps:
The recursive function for this challenge will use a binary search to find an element in a given array. If the inputted element is found then the function should return true. If it fails to find the element then it should return false.
binary_search([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], left, right, 7) ➞ True
binary_search([1, 11, 14, 15, 32, 64, 67, 88, 92, 94], left, right, 12) ➞ False
binary_search([3, 6, 9, 12, 15, 18, 21, 24, 27, 30], left, right, 27) ➞ True
//.