2

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

我正在按照伪代码在链接上实现算法,但不知道我的代码有什么问题。

这是我的代码:

/* 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);
4

4 回答 4

11

要从数组中获取值,您需要指定一个整数,例如array[1]. array[1.25]undefined在您的情况下返回。

为了让它工作,我只是Math.floor在你的循环中添加了以确保我们得到一个整数。

编辑:正如@KarelG 指出的那样,您还需要<=在 while 循环中添加。这适用于minmax变得相同的情况,在这种情况下guess === max === min。如果没有<=循环,在这些情况下将不会运行,并且函数将返回-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;
}

您可以使用Math.floorMath.ceil和中的任何一个Math.round

我希望这是一个小小的帮助,我不太擅长解释,但我会尽力详细说明。

于 2015-04-06T09:00:50.700 回答
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)

于 2015-04-06T08:56:00.203 回答
1

您只需要像这样取消注释 Program.assertEqual :

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

不像这样:

//Program.assertEqual(doSearch(primes, 73), 20);
于 2015-08-27T13:57:46.340 回答
0

如果有人仍在寻找答案,您需要完成它(最大值 >= 最小值)

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;
于 2015-06-29T14:45:42.100 回答