Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaScript中实现二进制搜索_Javascript_Algorithm_Binary Search_Bisection - Fatal编程技术网

如何在JavaScript中实现二进制搜索

如何在JavaScript中实现二进制搜索,javascript,algorithm,binary-search,bisection,Javascript,Algorithm,Binary Search,Bisection,我按照伪代码在链接上实现算法,但不知道我的代码出了什么问题 这是我的密码: /* 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

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

这是我的密码:

/* 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);
/*返回数组中位置的索引,
如果数组不包含targetValue,则为-1*/
var doSearch=函数(数组,targetValue){
var min=0;
var max=array.length-1;
var猜测;
同时(最小值<最大值){
猜测=(最大+最小)/2;
if(数组[猜测]==targetValue){
返回猜测;
}
else if(数组[猜测]
在代码中,当min等于max时,循环结束。但在这种情况下,您不会检查
array[min]==targetValue

因此,将代码更改为此最有可能解决您的问题

/* 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;
};
/*返回数组中位置的索引,
如果数组不包含targetValue,则为-1*/
var doSearch=函数(数组,targetValue){
var min=0;
var max=array.length-1;
var猜测;

而(min要从数组中获取值,您需要指定一个整数,如
数组[1]
数组[1.25]
在您的情况下将返回
未定义的

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


编辑:正如@KarelG所指出的,您还需要添加
如果有人仍在寻找答案,您需要添加答案(max>=min)

while(最大值>=min){
猜测=数学楼层((最大+最小)/2);
if(数组[猜测]==targetValue){
返回猜测;
}
else if(数组[猜测]
您只需取消对程序的注释。assertEqual 像这样:

Program.assertEqual(doSearch(primes, 73), 20);
//Program.assertEqual(doSearch(primes, 73), 20);
不是这样的:

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

while
之后,选中
array[min]==target
array[max]=target
,'println()'不是Javascript中的函数。我想您需要的是'console.log()''相反:有人能解释下一票以及为什么要删除此评论吗?我没有下一票,但是,请自己尝试代码。我怀疑它不起作用。
为什么不使用Math.round。请解释?@Begginer非常抱歉,我的不好。''Math.round'也一样好!编辑答案