Skip to content

Commit

Permalink
Merge pull request #4725 from andralex/sort
Browse files Browse the repository at this point in the history
Simplify, eliminate redundant work in sort
  • Loading branch information
dnadlinger authored Aug 12, 2016
2 parents 41a1d2b + 66ba7b8 commit 41df329
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions std/algorithm/sorting.d
Original file line number Diff line number Diff line change
Expand Up @@ -1336,28 +1336,27 @@ private void quickSortImpl(alias less, Range)(Range r, size_t depth)
const pivotIdx = getPivot!(less)(r);
auto pivot = r[pivotIdx];

alias pred = binaryFun!(less);

// partition
r.swapAt(pivotIdx, r.length - 1);
size_t lessI = size_t.max, greaterI = r.length - 1;

while (true)
outer: for (;;)
{
alias pred = binaryFun!less;
while (pred(r[++lessI], pivot)) {}
while (greaterI > 0 && pred(pivot, r[--greaterI])) {}

if (lessI >= greaterI)
assert(lessI <= greaterI, "sort: invalid comparison function.");
for (;;)
{
break;
if (greaterI == lessI) break outer;
if (!pred(pivot, r[--greaterI])) break;
}
assert(lessI <= greaterI, "sort: invalid comparison function.");
if (lessI == greaterI) break;
r.swapAt(lessI, greaterI);
}

r.swapAt(r.length - 1, lessI);
auto right = r[lessI + 1 .. r.length];

auto left = r[0 .. min(lessI, greaterI + 1)];
auto left = r[0 .. lessI], right = r[lessI + 1 .. r.length];
if (right.length > left.length)
{
swap(left, right);
Expand Down

0 comments on commit 41df329

Please sign in to comment.