-->

How to implement binary search in JavaScript

2020-02-15 02:32发布

问题:

https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/p/challenge-binary-search

I was following the pseudo code to implement algorithm on the link but don't know what's wrong with my code.

Here is my code :

/* Returns either the index of the location in the array,
  or -1 if the array did not contain the targetValue */

    var doSearch = function(array, targetValue) {
    var min = 0;
    var max = array.length - 1;
    var guess;

    while(min < max) {
        guess = (max + min) / 2;

        if (array[guess] === targetValue) {
            return guess;
        }
        else if (array[guess] < targetValue) {
            min = guess + 1;
        }
        else {
            max = guess - 1;
        }

    }

    return -1;
};

var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 
        41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];

var result = doSearch(primes, 2);
println("Found prime at index " + result);

//Program.assertEqual(doSearch(primes, 73), 20);

回答1:

To get a value from an array you need to specify a whole number like array[1]. array[1.25] will return undefined in your case.

To get it working I simply added Math.floor inside you loop to insure we get a whole number.

EDIT: As @KarelG pointet out you also need to add <= in your while loop. This is for situations where min and max have become the same, in which case guess === max === min. Without the <= the loop would not run in these situations and the function would return -1.

function (array, targetValue) {
    var min = 0;
    var max = array.length - 1;
    var guess;

    while(min <= max) {
        guess = Math.floor((max + min) / 2);

        if (array[guess] === targetValue) {
            return guess;
        }
        else if (array[guess] < targetValue) {
            min = guess + 1;
        }
        else {
            max = guess - 1;
        }

    }

    return -1;
}

You could use either of Math.floor, Math.ceil, and Math.round.

I hope this was a small help, I am not very good at explaining, but I'll do my be to elaborate.



回答2:

In your code when min is equal to max then the loop ends. But in this scenario you are not checking whether array[min] == targetValue

So changing the code to this will most likely fix your issue

/* Returns either the index of the location in the array,
  or -1 if the array did not contain the targetValue */

    var doSearch = function(array, targetValue) {
    var min = 0;
    var max = array.length - 1;
    var guess;

    while(min <= max) {
        guess = Math.floor((max + min) / 2);

        if (array[guess] === targetValue) {
            return guess;
        }
        else if (array[guess] < targetValue) {
            min = guess + 1;
        }
        else {
            max = guess - 1;
        }

    }

    return -1;
};

JSFiddle Link: http://jsfiddle.net/7zfph6ks/

Hope it helps.

PS: Only change in the code is this line: while (min <= max)



回答3:

you only have to uncomment the Program.assertEqual like this :

Program.assertEqual(doSearch(primes, 73), 20);

not like this :

//Program.assertEqual(doSearch(primes, 73), 20);


回答4:

If anyone is still looking for the answer, you needed to make it (max >= min)

while (max >= min) {
 guess = Math.floor((max + min) / 2);
 if (array[guess] === targetValue) {
     return guess;
 }
 else if (array[guess] < targetValue) {
     min = guess + 1;
 }
else {
    max = guess - 1;
    }
}
return -1;