Skip to content

Commit

Permalink
Tighter loop for insertion sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
andralex committed Sep 25, 2016
1 parent 97f58ea commit a439324
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions std/algorithm/sorting.d
Expand Up @@ -1326,16 +1326,17 @@ private void shortSort(alias less, Range)(Range r)
auto t = r[0]; if (pred(t, r[0])) r[0] = r[0];
}))) // Can we afford to temporarily invalidate the array?
{
size_t j = i;
if (pred(r[j + 1], r[j]))
size_t j = i + 1;
auto temp = r[i];
if (pred(r[j], temp))
{
auto temp = r[j];
do
{
r[j] = r[j + 1];
r[j - 1] = r[j];
++j;
}
while (++j + 1 < r.length && pred(r[j + 1], temp));
r[j] = temp;
while (j < r.length && pred(r[j], temp));
r[j - 1] = temp;
}
}
else
Expand Down

0 comments on commit a439324

Please sign in to comment.