mirror of
https://github.com/dlang/phobos.git
synced 2025-05-01 15:40:36 +03:00
Merge optimisticInsertionSort into a single function; Revise documentation for sort.
This commit is contained in:
parent
90c1a768c5
commit
650b03b56a
1 changed files with 36 additions and 35 deletions
|
@ -8906,10 +8906,8 @@ unittest
|
||||||
// sort
|
// sort
|
||||||
/**
|
/**
|
||||||
Sorts a random-access range according to the predicate $(D less). Performs
|
Sorts a random-access range according to the predicate $(D less). Performs
|
||||||
$(BIGOH r.length * log(r.length)) (if unstable) or $(BIGOH r.length *
|
$(BIGOH r.length * log(r.length)) evaluations of $(D less). Stable sorting
|
||||||
log(r.length) * log(r.length)) (if stable) evaluations of $(D less)
|
requires $(D hasAssignableElements!Range) to be true.
|
||||||
and $(D swap).
|
|
||||||
|
|
||||||
|
|
||||||
$(D sort) returns a $(XREF range, SortedRange) over the original range, which
|
$(D sort) returns a $(XREF range, SortedRange) over the original range, which
|
||||||
functions that can take advantage of sorted data can then use to know that the
|
functions that can take advantage of sorted data can then use to know that the
|
||||||
|
@ -8918,7 +8916,6 @@ wrapper around the original range, so both it and the original range are sorted,
|
||||||
but other functions won't know that the original range has been sorted, whereas
|
but other functions won't know that the original range has been sorted, whereas
|
||||||
they $(I can) know that $(XREF range, SortedRange) has been sorted.
|
they $(I can) know that $(XREF range, SortedRange) has been sorted.
|
||||||
|
|
||||||
|
|
||||||
The predicate is expected to satisfy certain rules in order for $(D sort) to
|
The predicate is expected to satisfy certain rules in order for $(D sort) to
|
||||||
behave as expected - otherwise, the program may fail on certain inputs (but not
|
behave as expected - otherwise, the program may fail on certain inputs (but not
|
||||||
others) when not compiled in release mode, due to the cursory $(D assumeSorted)
|
others) when not compiled in release mode, due to the cursory $(D assumeSorted)
|
||||||
|
@ -8928,13 +8925,22 @@ imply $(D !less(a,c)). Note that the default predicate ($(D "a < b")) does not
|
||||||
always satisfy these conditions for floating point types, because the expression
|
always satisfy these conditions for floating point types, because the expression
|
||||||
will always be $(D false) when either $(D a) or $(D b) is NaN.
|
will always be $(D false) when either $(D a) or $(D b) is NaN.
|
||||||
|
|
||||||
|
Returns: The initial range wrapped as a $(D SortedRange) with the predicate
|
||||||
|
$(D binaryFun!less).
|
||||||
|
|
||||||
|
Algorithms: $(WEB en.wikipedia.org/wiki/Introsort) is used for unstable sorting and
|
||||||
|
$(WEB en.wikipedia.org/wiki/Timsort, Timsort) is used for stable sorting.
|
||||||
|
Each algorithm has benefits beyond stability. Introsort is generally faster but
|
||||||
|
Timsort may achieve greater speeds on data with low entropy or if predicate calls
|
||||||
|
are expensive. Introsort performs no allocations whereas Timsort will perform one
|
||||||
|
or more allocations per call. Both algorithms have $(BIGOH n log n) worst-case
|
||||||
|
time complexity.
|
||||||
|
|
||||||
See_Also:
|
See_Also:
|
||||||
$(XREF range, assumeSorted)$(BR)
|
$(XREF range, assumeSorted)$(BR)
|
||||||
STL's $(WEB sgi.com/tech/stl/_sort.html, _sort)$(BR)
|
$(XREF range, SortedRange)$(BR)
|
||||||
$(WEB sgi.com/tech/stl/stable_sort.html, stable_sort)
|
$(XREF algorithm, SwapStrategy)$(BR)
|
||||||
|
$(XREF functional, binaryFun)
|
||||||
Remark: Stable sort is implementated as Timsort.
|
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
----
|
----
|
||||||
|
@ -9223,42 +9229,37 @@ private size_t getPivot(alias less, Range)(Range r)
|
||||||
}
|
}
|
||||||
|
|
||||||
private void optimisticInsertionSort(alias less, Range)(Range r)
|
private void optimisticInsertionSort(alias less, Range)(Range r)
|
||||||
if(hasAssignableElements!Range)
|
|
||||||
{
|
{
|
||||||
alias binaryFun!(less) pred;
|
alias binaryFun!(less) pred;
|
||||||
if (r.length < 2) {
|
if (r.length < 2)
|
||||||
return ;
|
{
|
||||||
}
|
return;
|
||||||
|
|
||||||
immutable maxJ = r.length - 1;
|
|
||||||
for (size_t i = r.length - 2; i != size_t.max; --i) {
|
|
||||||
size_t j = i;
|
|
||||||
auto temp = r[i];
|
|
||||||
|
|
||||||
for (; j < maxJ && pred(r[j + 1], temp); ++j) {
|
|
||||||
r[j] = r[j + 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
r[j] = temp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void optimisticInsertionSort(alias less, Range)(Range r)
|
|
||||||
if(!hasAssignableElements!Range)
|
|
||||||
{
|
|
||||||
alias binaryFun!(less) pred;
|
|
||||||
if (r.length < 2) {
|
|
||||||
return ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
immutable maxJ = r.length - 1;
|
immutable maxJ = r.length - 1;
|
||||||
for (size_t i = r.length - 2; i != size_t.max; --i)
|
for (size_t i = r.length - 2; i != size_t.max; --i)
|
||||||
{
|
{
|
||||||
for (size_t j = i; j < maxJ && pred(r[j + 1], r[j]); ++j)
|
size_t j = i;
|
||||||
|
|
||||||
|
static if (hasAssignableElements!Range)
|
||||||
|
{
|
||||||
|
auto temp = r[i];
|
||||||
|
|
||||||
|
for (; j < maxJ && pred(r[j + 1], temp); ++j)
|
||||||
|
{
|
||||||
|
r[j] = r[j + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
r[j] = temp;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (; j < maxJ && pred(r[j + 1], r[j]); ++j)
|
||||||
{
|
{
|
||||||
swapAt(r, j, j + 1);
|
swapAt(r, j, j + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unittest
|
unittest
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue