快速排序思绪

    1. Array.prototype.quickSortTwoPointers = function () {
    2. var sort = function (arr, startIdx, endIdx) {
    3. if(startIdx >= endIdx) return;
    4. var left = startIdx,
    5. right = endIdx,
    6. pivot = arr[startIdx];
    7. while (left < right) {
    8. while (left < right && arr[right] > pivot) {
    9. right--
    10. }
    11. while (left < right && arr[left] <= pivot) {
    12. left++
    13. }
    14. if (left < right) {
    15. var temp = arr[left];
    16. arr[left] = arr[right];
    17. arr[right] = temp
    18. }
    19. }
    20. if (left >= right) {
    21. arr[startIdx] = arr[left]
    22. arr[left] = pivot;
    23. sort(arr, startIdx, left);
    24. sort(arr, left + 1, arr.length -1);
    25. }
    26. }
    27. sort(this, 0, this.length -1);
    28. return this
    29. }