Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I fix my Binary Search code not working as expected?

I’m trying to perform my first binary search in javascript. But I’m failing to see what’s holding me back from getting the result I would expect from my code.

This is my code:

function search(arr, target, start = 0, end = arr.length - 1) {
  if (start > end) {
    console.log('Not found!');
    return -1;
  }

  const middle = Math.floor((start + end) / 2);

  if (arr[middle] === target) {
    console.log(`${target} found at index ${middle}`);
    return middle;
  }

  if (arr[middle] > target) {
    return search(target, start, middle - 1);
  }

  if (arr[middle] < target) {
    return search(target, middle + 1, end);
  }

}

const arr = ['a', 'b', 'c', 'x', 'y', 'z'];
console.log(search(arr, 'b'));

So when I run the above code my result is the following…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m getting
Not found!

What i’m expecting
b found at index 1
1

I’m fairly new to algorithms, but I’m failing to see what’s going on here. Even when just copying the code from the guide I’m following, I also get a different result then the teacher is getting. So there must be something underlying wrong I would assume?

Thanks in advance for taking time to help me out! I’m trying to learn and get a better understanding here. So please do elaborate if I’m totally misunderstanding the obvious here.

I’m running the program with node on my windows 10 machine and im using nodejs v18.14.1

>Solution :

The issue with your code is that you are not passing the entire array to the recursive calls of the search function.

In the cases where you are recursively calling the search function, you are only passing the target, start, and end parameters, but not the arr array itself. Therefore, the search function is not searching through the entire array, and this is causing it to return 'Not found!' even when the target is present in the array.

Try the following fix:

function search(arr, target, start = 0, end = arr.length - 1) {
  if (start > end) {
    console.log('Not found!');
    return -1;
  }

  const middle = Math.floor((start + end) / 2);

  if (arr[middle] === target) {
    console.log(`${target} found at index ${middle}`);
    return middle;
  }

  if (arr[middle] > target) {
    return search(arr, target, start, middle - 1);
  }

  if (arr[middle] < target) {
    return search(arr, target, middle + 1, end);
  }

}

const arr = ['a', 'b', 'c', 'x', 'y', 'z'];
console.log(search(arr, 'b'));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading