Skip to content

Instantly share code, notes, and snippets.

@tdubs42
Created February 20, 2024 00:45
Show Gist options
  • Select an option

  • Save tdubs42/5f9233f98393f9b7cbbe9eebc025cd15 to your computer and use it in GitHub Desktop.

Select an option

Save tdubs42/5f9233f98393f9b7cbbe9eebc025cd15 to your computer and use it in GitHub Desktop.

Revisions

  1. tdubs42 created this gist Feb 20, 2024.
    22 changes: 22 additions & 0 deletions binary-search-sorted-list.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    const binarySearch = (arr, target) => {
    let left = 0;
    let right = arr.length;

    while (right > left) {
    const indexToCheck = Math.floor((left + right) / 2);
    const checking = arr[indexToCheck];
    console.log(indexToCheck);

    if (checking === target) {
    return indexToCheck;
    } else if (checking < target) {
    left = indexToCheck + 1;
    } else {
    right = indexToCheck;
    }
    }

    return null;
    }

    module.exports = binarySearch;