小编Beg*_*ner的帖子

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

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 - …
Run Code Online (Sandbox Code Playgroud)

javascript algorithm binary-search bisection

2
推荐指数
1
解决办法
5604
查看次数

标签 统计

algorithm ×1

binary-search ×1

bisection ×1

javascript ×1