Javascript Array binaryIndexOf()

Description

Javascript Array binaryIndexOf()


function binaryIndexOf(searchElement, map) {
 'use strict';/*from w  w  w  .j  a v a2 s .  co  m*/

 if (map === undefined) map = index => this[index]

 var minIndex = 0;
 var maxIndex = this.length - 1;
 var currentIndex;
 var currentElement;
 var resultIndex;

 while (minIndex <= maxIndex) {
  resultIndex = currentIndex = (minIndex + maxIndex) / 2 | 0;
  currentElement = map(currentIndex);

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

 return ~maxIndex;
}

Array.prototype.binaryIndexOf = binaryIndexOf;



PreviousNext

Related