Skip to content

Instantly share code, notes, and snippets.

@schalkneethling
Created July 23, 2021 20:30
Show Gist options
  • Save schalkneethling/6cb1622cd0ae1fcbd42d091749a6a29f to your computer and use it in GitHub Desktop.
Save schalkneethling/6cb1622cd0ae1fcbd42d091749a6a29f to your computer and use it in GitHub Desktop.
A simple binary search implementation
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