Merge pull request #3987 from quickfur/schwartzSort-doc

Improve schwartzSort documentation.
This commit is contained in:
Robert Schadek 2016-02-12 18:53:03 +01:00
commit c548dbfbb4

View file

@ -998,6 +998,9 @@ 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.
Use $(XREF math, cmp) instead. Use $(XREF math, cmp) instead.
If `less` involves expensive computations on the _sort key, it may be
worthwhile to use $(LREF schwartzSort) instead.
Params: Params:
less = The predicate to sort by. less = The predicate to sort by.
ss = The swapping strategy to use. ss = The swapping strategy to use.
@ -2003,14 +2006,20 @@ unittest
// schwartzSort // schwartzSort
/** /**
Sorts a range using an algorithm akin to the $(WEB Alternative sorting method that should be used when comparing keys involves an
wikipedia.org/wiki/Schwartzian_transform, Schwartzian transform), also expensive computation. Instead of using `less(a, b)` for comparing elements,
known as the decorate-sort-undecorate pattern in Python and Lisp. `schwartzSort` uses `less(transform(a), transform(b))`. The values of the
This function is helpful when the sort comparison includes `transform` function are precomputed in a temporary array, thus saving on
an expensive computation. The complexity is the same as that of the repeatedly computing it. Conversely, if the cost of `transform` is small
corresponding $(D sort), but $(D schwartzSort) evaluates $(D compared to the cost of allocating and filling the precomputed array, `sort`
transform) only $(D r.length) times (less than half when compared to may be faster and therefore preferable.
regular sorting). The usage can be best illustrated with an example.
This approach to sorting is akin to the $(WEB
wikipedia.org/wiki/Schwartzian_transform, Schwartzian transform), also known as
the decorate-sort-undecorate pattern in Python and Lisp. The complexity is the
same as that of the corresponding `sort`, but `schwartzSort` evaluates
`transform` only `r.length` times (less than half when compared to regular
sorting). The usage can be best illustrated with an example.
Example: Example:
---- ----