快速排序思绪
Array.prototype.quickSortTwoPointers = function () {
var sort = function (arr, startIdx, endIdx) {
if(startIdx >= endIdx) return;
var left = startIdx,
right = endIdx,
pivot = arr[startIdx];
while (left < right) {
while (left < right && arr[right] > pivot) {
right--
}
while (left < right && arr[left] <= pivot) {
left++
}
if (left < right) {
var temp = arr[left];
arr[left] = arr[right];
arr[right] = temp
}
}
if (left >= right) {
arr[startIdx] = arr[left]
arr[left] = pivot;
sort(arr, startIdx, left);
sort(arr, left + 1, arr.length -1);
}
}
sort(this, 0, this.length -1);
return this
}