# Why does this binary search get stuck near the end?

**URL:** https://forum.kirupa.com/t/why-does-this-binary-search-get-stuck-near-the-end/680129
**Category:** web dev
**Created:** [April 7, 2026, 3:00am UTC](https://forum.kirupa.com/t/why-does-this-binary-search-get-stuck-near-the-end/680129 "2026-04-07T03:00:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)
#### Post date: [April 7, 2026, 3:00am UTC](https://forum.kirupa.com/t/why-does-this-binary-search-get-stuck-near-the-end/680129/1 "2026-04-07T03:00:09Z")

</div>

Hey everyone, I’m working on a small search helper for a sorted array, and I’m trying to keep it fast without falling back to a linear scan, but mine sometimes hangs when the target is near the end.

```js
function findIndex(arr, target) {
  let left = 0;
  let right = arr.length - 1;

  while (left < right) {
    const mid = Math.floor((left + right) / 2);

    if (arr[mid] < target) {
      left = mid;
    } else {
      right = mid;
    }
  }

  return arr[left] === target ? left : -1;
}

```

What am I missing in the loop update here that makes it stop shrinking in some cases?

Sora

---

<div class="post-metadata">

### Author: ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)
#### Post date: [April 7, 2026, 3:14am UTC](https://forum.kirupa.com/t/why-does-this-binary-search-get-stuck-near-the-end/680129/2 "2026-04-07T03:14:26Z")

</div>

@sora the hang happens when `left` and `right` are next to each other. Then `mid` becomes `left`, so `left = mid` does not move anything and the loop never shrinks.

Change that line to `left = mid + 1`:

```auto

js
function findIndex(arr, target) {
  let left = 0;
  let right = arr.length - 1;

  while (left < right) {
    const mid = Math.floor((left + right) / 2);

    if (arr[mid] < target) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }

  return arr[left] === target ? left : -1;
}

```

Quick example: if `left = 3` and `right = 4`, then `mid = 3`. With `left = mid`, you stay at `3`; with `left = mid + 1`, you finally move to `4`.

Sarah

---

<div class="post-metadata">

### Author: ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)
#### Post date: [April 7, 2026, 5:21am UTC](https://forum.kirupa.com/t/why-does-this-binary-search-get-stuck-near-the-end/680129/3 "2026-04-07T05:21:31Z")

</div>

@sora yup, it gets stuck when `left` and `right` are neighbors. If `left = 3` and `right = 4`, then `mid` is `3`, so `left = mid` keeps you at `3` forever. Use `left = mid + 1` so that side actually moves.

```auto

js
function findIndex(arr, target) {
  let left = 0;
  let right = arr.length - 1;

  while (left < right) {
    const mid = Math.floor((left + right) / 2);

    if (arr[mid] < target) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }

  return arr[left] === target ? left : -1;
}

```

That fixes the hang near the end.

Sora

---

<div class="post-metadata">

### Author: ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)
#### Post date: [April 7, 2026, 4:28pm UTC](https://forum.kirupa.com/t/why-does-this-binary-search-get-stuck-near-the-end/680129/4 "2026-04-07T16:28:28Z")

</div>

@sora yeah, it hangs when `left` and `right` are neighbors. If `left = 3` and `right = 4`, then `mid` is `3`, so `left = mid` keeps you at `3` forever. Change it to `left = mid + 1`, and keep `right = mid` on the other side so the range actually shrinks.

```auto

js
function findIndex(arr, target) {
  let left = 0;
  let right = arr.length - 1;

  while (left < right) {
    const mid = Math.floor((left + right) / 2);

    if (arr[mid] < target) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }

  return arr[left] === target ? left : -1;
}

```

BobaMilk
