| @@ 9738-9777 (lines=40) @@ | ||
| 9735 | // 1) for x in ]left, less[ : x < pivot. |
|
| 9736 | // 2) for x in [less, k[ : x == pivot. |
|
| 9737 | // 3) for x in ]great, right[ : x > pivot. |
|
| 9738 | for (var k = less; k <= great; ++k) { |
|
| 9739 | var ek = a[k], xk = f(ek); |
|
| 9740 | if (xk < pivotValue1) { |
|
| 9741 | if (k !== less) { |
|
| 9742 | a[k] = a[less]; |
|
| 9743 | a[less] = ek; |
|
| 9744 | } |
|
| 9745 | ++less; |
|
| 9746 | } else if (xk > pivotValue1) { |
|
| 9747 | ||
| 9748 | // Find the first element <= pivot in the range [k - 1, great] and |
|
| 9749 | // put [:ek:] there. We know that such an element must exist: |
|
| 9750 | // When k == less, then el3 (which is equal to pivot) lies in the |
|
| 9751 | // interval. Otherwise a[k - 1] == pivot and the search stops at k-1. |
|
| 9752 | // Note that in the latter case invariant 2 will be violated for a |
|
| 9753 | // short amount of time. The invariant will be restored when the |
|
| 9754 | // pivots are put into their final positions. |
|
| 9755 | while (true) { |
|
| 9756 | var greatValue = f(a[great]); |
|
| 9757 | if (greatValue > pivotValue1) { |
|
| 9758 | great--; |
|
| 9759 | // This is the only location in the while-loop where a new |
|
| 9760 | // iteration is started. |
|
| 9761 | continue; |
|
| 9762 | } else if (greatValue < pivotValue1) { |
|
| 9763 | // Triple exchange. |
|
| 9764 | a[k] = a[less]; |
|
| 9765 | a[less++] = a[great]; |
|
| 9766 | a[great--] = ek; |
|
| 9767 | break; |
|
| 9768 | } else { |
|
| 9769 | a[k] = a[great]; |
|
| 9770 | a[great--] = ek; |
|
| 9771 | // Note: if great < k then we will exit the outer loop and fix |
|
| 9772 | // invariant 2 (which we just violated). |
|
| 9773 | break; |
|
| 9774 | } |
|
| 9775 | } |
|
| 9776 | } |
|
| 9777 | } |
|
| 9778 | } else { |
|
| 9779 | ||
| 9780 | // We partition the list into three parts: |
|
| @@ 9885-9920 (lines=36) @@ | ||
| 9882 | // 1. for x in [ *, less[ : x == pivot1 |
|
| 9883 | // 2. for x in [less, k[ : pivot1 < x && x < pivot2 |
|
| 9884 | // 3. for x in ]great, * ] : x == pivot2 |
|
| 9885 | for (var k = less; k <= great; k++) { |
|
| 9886 | var ek = a[k], xk = f(ek); |
|
| 9887 | if (xk <= pivotValue1 && xk >= pivotValue1) { |
|
| 9888 | if (k !== less) { |
|
| 9889 | a[k] = a[less]; |
|
| 9890 | a[less] = ek; |
|
| 9891 | } |
|
| 9892 | less++; |
|
| 9893 | } else { |
|
| 9894 | if (xk <= pivotValue2 && xk >= pivotValue2) { |
|
| 9895 | while (true) { |
|
| 9896 | var greatValue = f(a[great]); |
|
| 9897 | if (greatValue <= pivotValue2 && greatValue >= pivotValue2) { |
|
| 9898 | great--; |
|
| 9899 | if (great < k) break; |
|
| 9900 | // This is the only location inside the loop where a new |
|
| 9901 | // iteration is started. |
|
| 9902 | continue; |
|
| 9903 | } else { |
|
| 9904 | // a[great] < pivot2. |
|
| 9905 | if (greatValue < pivotValue1) { |
|
| 9906 | // Triple exchange. |
|
| 9907 | a[k] = a[less]; |
|
| 9908 | a[less++] = a[great]; |
|
| 9909 | a[great--] = ek; |
|
| 9910 | } else { |
|
| 9911 | // a[great] == pivot1. |
|
| 9912 | a[k] = a[great]; |
|
| 9913 | a[great--] = ek; |
|
| 9914 | } |
|
| 9915 | break; |
|
| 9916 | } |
|
| 9917 | } |
|
| 9918 | } |
|
| 9919 | } |
|
| 9920 | } |
|
| 9921 | } |
|
| 9922 | ||
| 9923 | // The second partition has now been cleared of pivot elements and looks |
|
| @@ 9797-9832 (lines=36) @@ | ||
| 9794 | // 1. for x in ]left, less[ : x < pivot1 |
|
| 9795 | // 2. for x in [less, k[ : pivot1 <= x && x <= pivot2 |
|
| 9796 | // 3. for x in ]great, right[ : x > pivot2 |
|
| 9797 | for (var k = less; k <= great; k++) { |
|
| 9798 | var ek = a[k], xk = f(ek); |
|
| 9799 | if (xk < pivotValue1) { |
|
| 9800 | if (k !== less) { |
|
| 9801 | a[k] = a[less]; |
|
| 9802 | a[less] = ek; |
|
| 9803 | } |
|
| 9804 | ++less; |
|
| 9805 | } else { |
|
| 9806 | if (xk > pivotValue2) { |
|
| 9807 | while (true) { |
|
| 9808 | var greatValue = f(a[great]); |
|
| 9809 | if (greatValue > pivotValue2) { |
|
| 9810 | great--; |
|
| 9811 | if (great < k) break; |
|
| 9812 | // This is the only location inside the loop where a new |
|
| 9813 | // iteration is started. |
|
| 9814 | continue; |
|
| 9815 | } else { |
|
| 9816 | // a[great] <= pivot2. |
|
| 9817 | if (greatValue < pivotValue1) { |
|
| 9818 | // Triple exchange. |
|
| 9819 | a[k] = a[less]; |
|
| 9820 | a[less++] = a[great]; |
|
| 9821 | a[great--] = ek; |
|
| 9822 | } else { |
|
| 9823 | // a[great] >= pivot1. |
|
| 9824 | a[k] = a[great]; |
|
| 9825 | a[great--] = ek; |
|
| 9826 | } |
|
| 9827 | break; |
|
| 9828 | } |
|
| 9829 | } |
|
| 9830 | } |
|
| 9831 | } |
|
| 9832 | } |
|
| 9833 | } |
|
| 9834 | ||
| 9835 | // Move pivots into their final positions. |
|