Skip to content

Commit

Permalink
Revise sort.md and docstrings in sort.jl (#48363)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeremie Knuesel <knuesel@gmail.com>
(cherry picked from commit a134076)
  • Loading branch information
LilithHafner authored and nalimilan committed Nov 5, 2023
1 parent 7ea23a8 commit 8f1b02b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
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
48 changes: 23 additions & 25 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:

```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
on the values to be manipulated. The `isless` function is invoked by default, but the relation
can be specified via the `lt` keyword.
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, 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 information.

## Sorting Functions

Expand Down Expand Up @@ -165,22 +168,17 @@ 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 an order through the [`Base.Order.lt`](@ref)
function, which works as a generalization of `isless`.
This function's behavior on custom `Ordering`s must satisfy all the conditions of a
[strict weak order](https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings).
See [`sort!`](@ref) for details and examples of valid and invalid `lt` functions.

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

0 comments on commit 8f1b02b

Please sign in to comment.