Guest User

Untitled

a guest
Feb 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.34 KB | None | 0 0
  1. const binarySearch = (array, key) => {
  2. let lo = 0,
  3. hi = array.length - 1,
  4. mid,
  5. element;
  6. while (lo <= hi) {
  7. mid = Math.floor((lo + hi) / 2, 10);
  8. element = array[mid];
  9. if (element < key) {
  10. lo = mid + 1;
  11. } else if (element > key) {
  12. hi = mid - 1;
  13. } else {
  14. return mid;
  15. }
  16. }
  17. return -1;
  18. };
Add Comment
Please, Sign In to add comment