Created
December 19, 2022 17:51
-
-
Save 1solation/ad5a0596506d20a27e340835a99bcadf to your computer and use it in GitHub Desktop.
coding solution - binary search function in JS, with comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function binary_search(x, sorted_collection, low = 0, high = null) { | |
// x is target | |
// sorted_collection I will assume is always sorted in ascending numerical order AND starting from 1, due to the test cases | |
// low is the start index | |
// high is the end index | |
// initialise high to the last index of the array if it's not provided | |
if (high === null) { | |
high = sorted_collection.length - 1; // setting high as highest index, which start from 0 not 1 (hence the -1) | |
} else if (high < 0) { | |
throw new RangeError("High index out of range"); | |
} | |
// return null if low is greater than high | |
if (low > high) return null; | |
// calculate the midpoint | |
const mid = Math.floor((low + high) / 2); | |
const value = sorted_collection[mid]; | |
// return the value if it's equal to x, or recursively call binary_search to search the left or right halves of the array | |
if (value === x) { | |
// strict equality check | |
return mid; // or value - 1, if we returned value here (which was example code) we would always get `index +1` as we're starting our arr from 1 | |
} else if (value > x) { | |
return binary_search(x, sorted_collection, low, mid - 1); | |
} else if (value < x) { | |
// explicit else if to be more verbose, rather than an implicit else | |
return binary_search(x, sorted_collection, mid + 1, high); | |
} | |
} | |
// for unsorted array if present use the following code | |
// const sorted_arr = unsorted_arr.sort((a, b) => a - b); // numerically ascending order | |
/* | |
Time Complexity: | |
- avg case: O(log n) | |
- best case: the element to be search is in the middle of the list, requiring 1 comparison O(1) | |
- worst case: if the element is to search is in the first index or last index, or doesn't exist at all in the array then O(logN) | |
Space Complexity: | |
- iterative approach O(1) | |
- recursive approach (like this one is ) O(logN) | |
*/ | |
// Don't change anything below this line ;) | |
const tests = [ | |
{ | |
find: 1, | |
collection: [], | |
expected: null, | |
}, | |
{ | |
find: 1, | |
collection: [1], | |
expected: 0, | |
}, | |
{ | |
find: 2, | |
collection: [1], | |
expected: null, | |
}, | |
{ | |
find: 1, | |
collection: [1, 2], | |
expected: 0, | |
}, | |
{ | |
find: 2, | |
collection: [1, 2], | |
expected: 1, | |
}, | |
{ | |
find: 3, | |
collection: [1, 2, 3], | |
expected: 2, | |
}, | |
{ | |
find: 1, | |
collection: [1, 2, 3, 4], | |
expected: 0, | |
}, | |
{ | |
find: 2, | |
collection: [1, 2, 3, 4], | |
expected: 1, | |
}, | |
{ | |
find: 3, | |
collection: [1, 2, 3, 4], | |
expected: 2, | |
}, | |
{ | |
find: 4, | |
collection: [1, 2, 3, 4], | |
expected: 3, | |
}, | |
{ | |
find: 1, | |
collection: [1, 2, 3, 4, 5], | |
expected: 0, | |
}, | |
{ | |
find: 2, | |
collection: [1, 2, 3, 4, 5], | |
expected: 1, | |
}, | |
{ | |
find: 3, | |
collection: [1, 2, 3, 4, 5], | |
expected: 2, | |
}, | |
{ | |
find: 4, | |
collection: [1, 2, 3, 4, 5], | |
expected: 3, | |
}, | |
{ | |
find: 5, | |
collection: [1, 2, 3, 4, 5], | |
expected: 4, | |
}, | |
{ | |
find: 99, | |
collection: [1, 2, 3, 4, 5], | |
expected: null, | |
}, | |
]; | |
const results = tests.map(function (test) { | |
try { | |
var result = binary_search(test["find"], test["collection"]); | |
test.result = result; | |
test.success = result === test["expected"]; | |
} catch (ex) { | |
test.result = ex.message; | |
test.success = false; | |
} | |
return test; | |
}); | |
console.table(results); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment