【问题标题】:How to implement binary search in JavaScript如何在 JavaScript 中实现二分查找
【发布时间】:2015-06-10 15:50:48
【问题描述】:

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);

【问题讨论】:

标签: javascript algorithm binary-search bisection


【解决方案1】:

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

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

不是这样的:

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

【讨论】:

    【解决方案2】:

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

    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;
    

    【讨论】:

      【解决方案3】:

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

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

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

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

      【讨论】:

      • &lt;= 在需要 while 循环。通过搜索 11 尝试您的代码。
      • 为什么不用 Math.round。请解释一下?
      • @Begginer 非常抱歉,我的错。 'Math.round' 一样好!编辑答案
      【解决方案4】:

      在您的代码中,当 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;
      };
      

      JSFiddle 链接:http://jsfiddle.net/7zfph6ks/

      希望对你有帮助。

      PS: 代码中唯一的变化是这一行:while (min &lt;= max)

      【讨论】:

      • 有人能解释一下反对意见以及为什么要删除这条评论吗?
      • 我没有投反对票,但是,自己试试代码。我怀疑它不起作用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      • 2010-10-28
      • 2019-12-25
      • 1970-01-01
      • 2018-04-13
      • 1970-01-01
      相关资源
      最近更新 更多