You will be able to participate in the social aspects of GitHub by starring repositories, following other developers, and reviewing your followers
Explain the complexity of and write a function that performs a binary search on a sorted array of numbers.
Implement recursive binary search that returns true/false if the target number is present
Code:
// Returns simply true/false for presence
function binarySearchRecursive(array, target) {
// Our base case
// If our array is empty, we do not have the target
if (array.length === 0) {
return false;
}
// Get a reference to the middle index, ie what we want to check
let midIdx = Math.floor(array.length / 2);
// If our target is smaller than the middle element, repeat this process with
// the left half of our array.
// We get a subarray that represents our left half by slicing up to but not
// including our midIdx.
if (target < array[midIdx]) {
let leftHalf = array.slice(0, midIdx);
return binarySearch(leftHalf, target);
// If our target is larger than the middle element, repeat this process with
// the right half of our array.
// We get a subarray that represents our right half by slicing from the
// midIdx + 1 all the way to the end of our array (no second argument needed).
} else if (target > array[midIdx]) {
let rightHalf = array.slice(midIdx + 1);
return binarySearch(rightHalf, target);
// If neither of these occurred, we found our element and return true.
} else {
return true;
}
}Implement iterative binary search that returns true/false if the target number is present
Code:
// Returns simply true/false for presence
function binarySearchIterative(array, target) {
// Get a reference to our lower and upper bounds that we would like to search
// within our array. At the start, this is the entire array, so the indices are
// 0 and our length - 1.
let lowerIdx = 0;
let upperIdx = array.length - 1;
// We also create a midIdx variable because we will reassign it at each iteration
let midIdx;
// While our lowerIdx <= upperIdx, we still have elements that we haven't ruled
// out as being our target, so we want our iteration to continue.
while (lowerIdx <= upperIdx) {
// Get a reference to the middle element within our current bounds.
// We are using Math.floor in order to get an integer/valid index.
// (If we used ceiling, we would have to do some subtraction in order to get
// our first element. For example, [14] has a length 1, so
// Math.ceil((0 + 1)/2)) = 1, which is outside our bounds.
midIdx = Math.floor((lowerIdx + upperIdx) / 2);
// If our target is larger than our current middle element, our lower bound
// needs to be moved up past our midIdx so that we look at the right half.
if (array[midIdx] < target) {
lowerIdx = midIdx + 1;
// If our target is smaller than our current middle element, our upper bound
// needs to be moved down past our midIdx so that we look at the left half.
} else if (array[midIdx] > target) {
upperIdx = midIdx - 1;
// Otherwise, we have found our target at the midIdx and can return true.
} else {
return true;
}
}
// If we made it outside of our loop without returning, our target is not in
// the array, so we can return false.
return false;
}Implement iterative binary search that returns the index or -1 if not found
Code:
// Returns the index or -1 if not found
function binarySearchIndexIterative(array) {
// The implementation of this function is exactly the same as returning a boolean
// Instead of returning true/false, we return the midIdx.
// because we are never making subarrays; we are only dealing with the indices
// as they relate to the original array.
let lowerIdx = 0;
let upperIdx = array.length - 1;
let midIdx;
while (lowerIdx <= upperIdx) {
midIdx = Math.floor((lowerIdx + upperIdx) / 2);
if (array[midIdx] < target) {
lowerIdx = midIdx + 1;
} else if (array[midIdx] > target) {
upperIdx = midIdx - 1;
} else {
return midIdx;
}
}
return -1;
}