Nodejs Array Search Binary binaryIndexOf(searchElement)

Here you can find the source of binaryIndexOf(searchElement)

Method Source Code

function binaryIndexOf(){

/**// www . j  a  v  a 2 s.c o m
 * \from: [https://gist.github.com/Wolfy87/5734530]
 * Performs a binary search on the host array. This method can either be
 * injected into Array.prototype or called with a specified scope like this:
 * binaryIndexOf.call(someArray, searchElement);
 *
 *
 * @param {*} searchElement The item to search for within the array.
 * @return {Number} The index of the element which defaults to -1 when not
 * found.
 */
Array.prototype.binaryIndexOf = function(searchElement) {
    var minIndex = 0;
    var maxIndex = this.length - 1;
    var currentIndex;
    var currentElement;

    while (minIndex <= maxIndex) {
        currentIndex = Math.floor((minIndex + maxIndex) / 2);
        currentElement = this[currentIndex];
        if (currentElement.compare(searchElement) < 0) {
            minIndex = currentIndex + 1;
        }
        else if (currentElement.compare(searchElement) > 0) {
            maxIndex = currentIndex - 1;
        }
        else {
            return currentIndex;
        }
    };
    return ~maxIndex;
};

}

module.exports = binaryIndexOf();

Related

  1. binSearch(data)
    Array.prototype.binSearch = function(data) {
      var first = 0;
      var last = this.length - 1;
      while(first <= last) {
        var mid = Math.floor((first + last) / 2);
        if(this[mid] < data) {
          first = mid + 1;
        else if(this[mid] > data) {
    ...
    
  2. 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) {
    ...
    
  3. 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));
      });
    });
    
  4. 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);
    ...
    
  5. 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];
    ...