Javascript Array partition(start, len)

Description

Javascript Array partition(start, len)


Array.prototype.partition = function (start, len) {
  var endIdx = len + startIdx - 1;
  if (endIdx >= this.length) throw new Error("Index out of bounds");
  var pivotIdx = startIdx;
  while (++startIdx <= endIdx) {
    if (this[startIdx] < this[pivotIdx]) {
      var tmp = this[pivotIdx + 1];
      this[pivotIdx + 1] = this[startIdx];
      this[startIdx] = tmp;/*from  w  ww  .java2  s.  c o m*/
      var pivot = this[pivotIdx];
      this[pivotIdx] = this[pivotIdx + 1];
      this[pivotIdx + 1] = pivot;
      pivotIdx++;
    }
  }
  return pivotIdx;
};



PreviousNext

Related