zoukankan      html  css  js  c++  java
  • javascript二分法

    //Copyright 2009 Nicholas C. Zakas. All rights reserved.
    //MIT-Licensed, see source file
    function binarySearch(items, value){
     
        var startIndex  = 0,
            stopIndex   = items.length - 1,
            middle      = Math.floor((stopIndex + startIndex)/2);
     
        while(items[middle] != value && startIndex < stopIndex){
     
            //adjust search area(调整查找范围)
            if (value < items[middle]){
                stopIndex = middle - 1;
            } else if (value > items[middle]){
                startIndex = middle + 1;
            }
     
            //recalculate middle(重新计算中项索引)
            middle = Math.floor((stopIndex + startIndex)/2);
        }
     
        //make sure it's the right value(确保返回正确的值)
        return (items[middle] != value) ? -1 : middle;
    }
  • 相关阅读:
    Polly
    ELK
    Python基础三(选择,循环)
    Python基础二(输入与输出)
    Python关键字
    Python基础一(基本类型和运算符)
    Python发展史
    在虚拟机(vmware)上安装CentOS
    centos7联网
    Hashmap的实现
  • 原文地址:https://www.cnblogs.com/benio/p/1718650.html
Copyright © 2011-2022 走看看