code_binary_search.js 620 B

123456789101112131415161718192021222324252627282930
  1. // Generated by CoffeeScript 1.6.1
  2. (function() {
  3. window.binary_search = function(items, value) {
  4. var pivot, start, stop;
  5. start = 0;
  6. stop = items.length - 1;
  7. pivot = Math.floor((start + stop) / 2);
  8. while (items[pivot] !== value && start < stop) {
  9. if (value < items[pivot]) {
  10. stop = pivot - 1;
  11. }
  12. if (value > items[pivot]) {
  13. start = pivot + 1;
  14. }
  15. pivot = Math.floor((stop + start) / 2);
  16. }
  17. if (items[pivot] === value) {
  18. return pivot;
  19. } else {
  20. return -1;
  21. }
  22. };
  23. }).call(this);
  24. /*
  25. //# sourceMappingURL=code_binary_search.map
  26. */