Created
July 23, 2021 20:30
-
-
Save schalkneethling/6cb1622cd0ae1fcbd42d091749a6a29f to your computer and use it in GitHub Desktop.
A simple binary search implementation
This file contains hidden or 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 binarySearch(id, array) { | |
const mid = Math.floor(array.length / 2); | |
if (array[mid].id === id) { | |
return array[mid]; | |
} else if (array[mid].id < id) { | |
return binarySearch(id, array.slice(mid)); | |
} else { | |
return binarySearch(id, array.slice(0, mid)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment