Nodejs Array Search Binary binarySearch(val)

Here you can find the source of binarySearch(val)

Method Source Code

Array.prototype.binarySearch = function(val){
  var keeper = 0;
  var answer = -1;
  function recursive(arr){
    var len = arr.length;
    var start = Math.floor(len / 2);
    var current = arr[start];
    if (current === val){
      answer = start + keeper;/* w ww.j  a  v  a  2 s. c o  m*/
    } else if (current > val) {
      console.log(arr.slice(0, start));
      recursive(arr.slice(0, start));
    } else if (current < val){
      console.log(arr.slice(start, len));
      keeper += start;
      recursive(arr.slice(start, len));
    }
  }
  recursive(this);
  return answer;
};

module.exports = { Array };

Related

  1. binaryIndexOfbinaryIndexOf;
    function binaryIndexOf(searchElement, map) {
      'use strict';
      if (map === undefined) map = index => this[index]
      var minIndex = 0;
      var maxIndex = this.length - 1;
      var currentIndex;
      var currentElement;
      var resultIndex;
      while (minIndex <= maxIndex) {
    ...
    
  2. binarySearch(n)
    "use strict";
    var assert = require('assert'); 
    Array.prototype.binarySearch = function(n) {
      var O = Object(this);
      var f = function(lo,hi) {
        var mid = Math.floor((lo + hi) / 2);
        if (O[mid] === n) {
          return mid;
        if (lo === mid || hi === mid) {
          return -(mid+1);
        if(O[mid] < n) {
          return f(mid,hi);
        if (O[mid] > n) {
          return f(lo,mid);
      };
      return f(0, O.length);
    };
    describe('binary search', function() {
      it('doesnt find anything in an empty list', function() {
        assert.equal(-1, [].binarySearch(7));
      });
      it('finds a single element', function() {
        assert.equal(0, [0].binarySearch(0));
      });
      it('finds the middle element', function() {
        assert.equal(1, [1,2,3].binarySearch(2));
      });
      it('finds up', function() {
        assert.equal(2, [1,2,3].binarySearch(3));
      });
      it('finds down', function() {
        assert.equal(0, [1,2,3].binarySearch(1));
      });
      it('finds insertion index', function() {
        assert.equal(-2, [1,2,4,5,6].binarySearch(3));
      });
    });
    
  3. binarySearch(num)
    var input = [ 1, 2, 2, 3, 4, 4, 5, 6, 8, 10 ];
    Array.prototype.binarySearch = function(num) {
        var _this = this;
        function search(left, right) {
            if(right < left) return -1;
            else if(left === right) {
                return checkIndexForNum(left);
            var mid = Math.floor((left + right) / 2);
    ...
    
  4. binarySearch(searchElement)
    Array.prototype.binarySearch = function (searchElement) {
        'use strict';
        var minIndex = 0;
        var maxIndex = this.length - 1;
        var currentIndex;
        var currentElement;
        while (minIndex <= maxIndex) {
            currentIndex = (minIndex + maxIndex) / 2 | 0;
            currentElement = this[currentIndex];
    ...
    
  5. binarySearch(val)
    Array.prototype.binarySearch = function(val){
      var min = 0;
      var max = this.length -1;
      var currentIndex;
      var currentElement;
      while (min <= max) {
        currentIndex = Math.floor((min +max) / 2);
        currentElement = this[currentIndex];
        if (currentElement > val) {
    ...
    
  6. binarySearch(value)
    Array.prototype.binarySearch = function(value) {
        'use strict';
        if(!value){
            return null;
        var left = 0,
            right = this.length-1,
            find = false,
            middle = null;
    ...
    
  7. binarySearch(value)
    Array.prototype.binarySearch = function(value){
        var items = this;
        console.log(items.length);
        var startIndex  = 0;
        var stopIndex = items.length - 1;
        var middle = Math.floor((stopIndex + startIndex)/2);
        while(items[middle] != value && startIndex < stopIndex){
            if (value < items[middle]){
                stopIndex = middle - 1;
    ...
    
  8. binarySearchbinaryIndexOf;
    function binaryIndexOf(searchElement) {
        'use strict';
        var minIndex = 0;
        var maxIndex = this.length - 1;
        var currentIndex = -1;
        var currentElement;
        while (minIndex <= maxIndex) {
            currentIndex = (minIndex + maxIndex) / 2 | 0;
            currentElement = this[currentIndex];
    ...
    
  9. binarySearchbinarySearch(val, from, to)
    Array.prototype.binarySearch = function binarySearch(val, from, to){
        if (from === undefined) {
            from = 0;
        if (to === undefined) {
            to = this.length-1;
        var mid = Math.ceil((from+to)/2),
            midVal = this[mid];
    ...