Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BinarySearch #1

Open
barretlee opened this issue May 24, 2016 · 3 comments
Open

BinarySearch #1

barretlee opened this issue May 24, 2016 · 3 comments

Comments

@barretlee
Copy link
Owner

/chapter-1-fundamentals/1.1-programming-model/BinarySearch.js

@barretlee
Copy link
Owner Author

barretlee commented May 24, 2016

function BinarySearch(input, key) {
  return indexOf(input, key);

  function indexOf(a, k) {
    var start = 0;
    var end = a.length - 1;
    while(start <= end) {
      var mid = Math.floor((end - start) / 2) + start;
      if(k < a[mid]) {
        end = mid - 1;
      } else if(k > a[mid]) {
        start = mid + 1;
      } else {
        return mid;
      }
    }
    return -1;
  }
}

@lessfish
Copy link

小胡子哥习惯在函数声明最后面加个封号么?

@barretlee
Copy link
Owner Author

@hanzichi 分号去掉了,不是习惯,可能是复制的时候多写了一个。

barretlee pushed a commit that referenced this issue Aug 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants