Javascript Array search(n)

Description

Javascript Array search(n)

Array.prototype.search = function(n) {
  var start = 0;/* www . j  a v a  2s.  co m*/
  var end = this.length - 1;
  var result = {count: 0, index: -1, length: this.length};
  while (start <= end) {
    mid = (start + end) / 2 >> 0;
    if (this[start] === n) {
      result.index = start;
      return result;
    } else if (this[end] === n) {
      result.index = end;
      return result;
    } else if (this[mid] === n) {
      result.index = mid;
      return result;
    } else if (this[mid] > n) {
      end = mid - 1;
      start += 1;
    } else {
      start = mid + 1;
      end -= 1;
    }
    result.count += 1;
  }
  return result;
};



PreviousNext

Related