Nodejs Array Search Binary binarySearch(value)

Here you can find the source of binarySearch(value)

Method Source Code

Array.prototype.binarySearch = function(value){
    var items = this;
    console.log(items.length);/*  w w w . ja v a 2s .c o m*/
    var startIndex  = 0;
    var stopIndex = items.length - 1;
    var middle = Math.floor((stopIndex + startIndex)/2);

    while(items[middle] != value && startIndex < stopIndex){

        //adjust search area
        if (value < items[middle]){
            stopIndex = middle - 1;
        } 
        if (value > items[middle]){
            startIndex = middle + 1;
        }

        //recalculate middle
        middle = Math.floor((stopIndex + startIndex)/2);
    }

    //make sure it's the right value
    return (items[middle] != value) ? console.log(-1) : console.log(middle);
}

sam = [6,7,8,5,3, 4, 2,9,1]
sam.binarySearch(7);

Related

  1. 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);
    ...
    
  2. 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];
    ...
    
  3. 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) {
    ...
    
  4. 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;
    ...
    
  5. binarySearch(value)
    Array.prototype.binarySearch = function(value) {
        'use strict';
        if(!value){
            return null;
        var left = 0,
            right = this.length-1,
            find = false,
            middle = null;
    ...
    
  6. 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];
    ...
    
  7. 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];
    ...
    
  8. 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);
    ...
    
  9. 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;
    };