Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise sort.md and docstrings in sort.jl #48363

Merged
merged 11 commits into from
Jul 10, 2023
8 changes: 4 additions & 4 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2013,9 +2013,9 @@ struct MergeSortAlg <: Algorithm end
"""
PartialQuickSort{T <: Union{Integer,OrdinalRange}}

Indicate that a sorting function should use the partial quick sort
algorithm. Partial quick sort returns the smallest `k` elements sorted from smallest
to largest, finding them and sorting them using [`QuickSort`](@ref).
Indicate that a sorting function should use the partial quick sort algorithm.
`PartialQuickSort(k)` is like `QuickSort`, but is only required to find and
sort the elements that would end up in `v[k]` were `v` fully sorted.

Characteristics:
* *not stable*: does not preserve the ordering of elements that
Expand All @@ -2024,7 +2024,7 @@ Characteristics:
* *in-place* in memory.
* *divide-and-conquer*: sort strategy similar to [`MergeSort`](@ref).

Note that `PartialQuickSort(k)` does not necessarily sort the whole array. For example,
Note that `PartialQuickSort(k)` does not necessarily sort the whole array. For example,

```jldoctest
julia> x = rand(100);
Expand Down
50 changes: 26 additions & 24 deletions doc/src/base/sort.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Sorting and Related Functions

Julia has an extensive, flexible API for sorting and interacting with already-sorted arrays of
values. By default, Julia picks reasonable algorithms and sorts in standard ascending order:
Julia has an extensive, flexible API for sorting and interacting with already-sorted arrays
of values. By default, Julia picks reasonable algorithms and sorts in ascending order:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"standard" is a bit presumptuous imo. I hope what we chose as Julia's standard is also what the user things of as standard, but maybe they are used to seeing big things first. "ascending" is the more useful and unambiguous descriptor here.


```jldoctest
julia> sort([2,3,1])
Expand All @@ -11,7 +11,7 @@ julia> sort([2,3,1])
3
```

You can easily sort in reverse order as well:
You can sort in reverse order as well:

```jldoctest
julia> sort([2,3,1], rev=true)
Expand All @@ -36,8 +36,8 @@ julia> a
3
```

Instead of directly sorting an array, you can compute a permutation of the array's indices that
puts the array into sorted order:
Instead of directly sorting an array, you can compute a permutation of the array's
indices that puts the array into sorted order:

```julia-repl
julia> v = randn(5)
Expand Down Expand Up @@ -65,7 +65,7 @@ julia> v[p]
0.382396
```

Arrays can easily be sorted according to an arbitrary transformation of their values:
Arrays can be sorted according to an arbitrary transformation of their values:

```julia-repl
julia> sort(v, by=abs)
Expand Down Expand Up @@ -101,9 +101,12 @@ julia> sort(v, alg=InsertionSort)
0.382396
```

All the sorting and order related functions rely on a "less than" relation defining a total order
All the sorting and order related functions rely on a "less than" relation defining a
[strict weak order](https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings)
on the values to be manipulated. The `isless` function is invoked by default, but the relation
can be specified via the `lt` keyword.
can be specified via the `lt` keyword, a function that takes two array elements and returns `true`
if and only if the first argument is "less than" the second. See [`sort!`](@ref) and [Alternate orderings](@ref) for
more info.
LilithHafner marked this conversation as resolved.
Show resolved Hide resolved

## Sorting Functions

Expand Down Expand Up @@ -165,22 +168,21 @@ Base.Sort.defalg(::AbstractArray{<:Union{SmallInlineStrings, Missing}}) = Inline

## Alternate Orderings

By default, `sort` and related functions use [`isless`](@ref) to compare two
elements in order to determine which should come first. The
[`Base.Order.Ordering`](@ref) abstract type provides a mechanism for defining
alternate orderings on the same set of elements: when calling a sorting function like
`sort`, an instance of `Ordering` can be provided with the keyword argument `order`.

Instances of `Ordering` define a [total order](https://en.wikipedia.org/wiki/Total_order)
on a set of elements, so that for any elements `a`, `b`, `c` the following hold:

* Exactly one of the following is true: `a` is less than `b`, `b` is less than
`a`, or `a` and `b` are equal (according to [`isequal`](@ref)).
* The relation is transitive - if `a` is less than `b` and `b` is less than `c`
then `a` is less than `c`.

The [`Base.Order.lt`](@ref) function works as a generalization of `isless` to
test whether `a` is less than `b` according to a given order.
By default, `sort`, `searchsorted`, and related functions use [`isless`](@ref) to compare
two elements in order to determine which should come first. The
[`Base.Order.Ordering`](@ref) abstract type provides a mechanism for defining alternate
orderings on the same set of elements: when calling a sorting function like
`sort!`, an instance of `Ordering` can be provided with the keyword argument `order`.

Instances of `Ordering` define a
[strict weak order](https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings)
through the [`Base.Order.lt`](@ref) function, which works as
a generalization of `isless`.
For `lt` to be a strict weak order, for any elements `a`, `b`, `c` the following must hold:

* `lt(a, b) && lt(b, a) === false`;
* if `lt(a, b) && lt(b, c)`, then `lt(a, c)`; and
* if `!lt(a, b) && !lt(b, c)`, then `!lt(a, c)`
LilithHafner marked this conversation as resolved.
Show resolved Hide resolved

```@docs
Base.Order.Ordering
Expand Down