Nodejs Array Search Binary binarySearchbinaryIndexOf;

Here you can find the source of binarySearchbinaryIndexOf;

Method Source Code

/**//ww  w .j  a v  a  2s  .c  o m
 * 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.
 */
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];

        if (currentElement < searchElement) {
            minIndex = currentIndex + 1;
        }
        else if (currentElement > searchElement) {
            maxIndex = currentIndex - 1;
        }
        else {
            return currentIndex;
        }
    }

    return currentIndex;
}

Array.prototype.binarySearch = binaryIndexOf;

Related

  1. 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];
    ...
    
  2. 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) {
    ...
    
  3. binarySearch(val)
    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;
    ...
    
  4. binarySearch(value)
    Array.prototype.binarySearch = function(value) {
        'use strict';
        if(!value){
            return null;
        var left = 0,
            right = this.length-1,
            find = false,
            middle = null;
    ...
    
  5. 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;
    ...
    
  6. 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];
    ...
    
  7. binary_search(val, left, right)
    Array.prototype.binary_search = function(val, left, right) {
        if (typeof left === 'undefined') left = 0;
        if (typeof right === 'undefined') right = this.length - 1;
        if (left > right) return null;
        var mid = (left + right) >> 1;
        if (val == this[mid]) {
            return mid;
        } else if (val > this[mid]) {
            return this.binary_search(val, mid + 1, right);
    ...
    
  8. binarysearch(i)
    Array.prototype.binarysearch = function (i) {
        var l = 0, u = this.length,  m;
        while ( l <= u ) {
            m = ((l+u) >> 1);
            if ( i > this[m].i ) {
                l = m+1;
            } else {
                u = (i == this[m]) ? -2 : m - 1;
        return (u == -2) ? m : -1;
    };
    
  9. bsearch(target)
    Array.prototype.bsearch = function (target) {
      var half = parseInt(this.length / 2);
      if (target === this[half]) {
        return half;
      if (target > this[half]) {
        return half + this.slice(half,this.length).bsearch(target);
      } else {
        return this.slice(0, half).bsearch(target);
    ...