Skip to content

Commit a134076

Browse files
Revise sort.md and docstrings in sort.jl (#48363)
Co-authored-by: Jeremie Knuesel <[email protected]>
1 parent 8dd58a4 commit a134076

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

base/sort.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,9 +2013,9 @@ struct MergeSortAlg <: Algorithm end
20132013
"""
20142014
PartialQuickSort{T <: Union{Integer,OrdinalRange}}
20152015
2016-
Indicate that a sorting function should use the partial quick sort
2017-
algorithm. Partial quick sort returns the smallest `k` elements sorted from smallest
2018-
to largest, finding them and sorting them using [`QuickSort`](@ref).
2016+
Indicate that a sorting function should use the partial quick sort algorithm.
2017+
`PartialQuickSort(k)` is like `QuickSort`, but is only required to find and
2018+
sort the elements that would end up in `v[k]` were `v` fully sorted.
20192019
20202020
Characteristics:
20212021
* *not stable*: does not preserve the ordering of elements that
@@ -2024,7 +2024,7 @@ Characteristics:
20242024
* *in-place* in memory.
20252025
* *divide-and-conquer*: sort strategy similar to [`MergeSort`](@ref).
20262026
2027-
Note that `PartialQuickSort(k)` does not necessarily sort the whole array. For example,
2027+
Note that `PartialQuickSort(k)` does not necessarily sort the whole array. For example,
20282028
20292029
```jldoctest
20302030
julia> x = rand(100);

doc/src/base/sort.md

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Sorting and Related Functions
22

3-
Julia has an extensive, flexible API for sorting and interacting with already-sorted arrays of
4-
values. By default, Julia picks reasonable algorithms and sorts in standard ascending order:
3+
Julia has an extensive, flexible API for sorting and interacting with already-sorted arrays
4+
of values. By default, Julia picks reasonable algorithms and sorts in ascending order:
55

66
```jldoctest
77
julia> sort([2,3,1])
@@ -11,7 +11,7 @@ julia> sort([2,3,1])
1111
3
1212
```
1313

14-
You can easily sort in reverse order as well:
14+
You can sort in reverse order as well:
1515

1616
```jldoctest
1717
julia> sort([2,3,1], rev=true)
@@ -36,8 +36,8 @@ julia> a
3636
3
3737
```
3838

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

4242
```julia-repl
4343
julia> v = randn(5)
@@ -65,7 +65,7 @@ julia> v[p]
6565
0.382396
6666
```
6767

68-
Arrays can easily be sorted according to an arbitrary transformation of their values:
68+
Arrays can be sorted according to an arbitrary transformation of their values:
6969

7070
```julia-repl
7171
julia> sort(v, by=abs)
@@ -101,9 +101,12 @@ julia> sort(v, alg=InsertionSort)
101101
0.382396
102102
```
103103

104-
All the sorting and order related functions rely on a "less than" relation defining a total order
105-
on the values to be manipulated. The `isless` function is invoked by default, but the relation
106-
can be specified via the `lt` keyword.
104+
All the sorting and order related functions rely on a "less than" relation defining a
105+
[strict weak order](https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings)
106+
on the values to be manipulated. The `isless` function is invoked by default, but the
107+
relation can be specified via the `lt` keyword, a function that takes two array elements
108+
and returns `true` if and only if the first argument is "less than" the second. See
109+
[`sort!`](@ref) and [Alternate Orderings](@ref) for more information.
107110

108111
## Sorting Functions
109112

@@ -165,22 +168,17 @@ Base.Sort.defalg(::AbstractArray{<:Union{SmallInlineStrings, Missing}}) = Inline
165168

166169
## Alternate Orderings
167170

168-
By default, `sort` and related functions use [`isless`](@ref) to compare two
169-
elements in order to determine which should come first. The
170-
[`Base.Order.Ordering`](@ref) abstract type provides a mechanism for defining
171-
alternate orderings on the same set of elements: when calling a sorting function like
172-
`sort`, an instance of `Ordering` can be provided with the keyword argument `order`.
173-
174-
Instances of `Ordering` define a [total order](https://en.wikipedia.org/wiki/Total_order)
175-
on a set of elements, so that for any elements `a`, `b`, `c` the following hold:
176-
177-
* Exactly one of the following is true: `a` is less than `b`, `b` is less than
178-
`a`, or `a` and `b` are equal (according to [`isequal`](@ref)).
179-
* The relation is transitive - if `a` is less than `b` and `b` is less than `c`
180-
then `a` is less than `c`.
181-
182-
The [`Base.Order.lt`](@ref) function works as a generalization of `isless` to
183-
test whether `a` is less than `b` according to a given order.
171+
By default, `sort`, `searchsorted`, and related functions use [`isless`](@ref) to compare
172+
two elements in order to determine which should come first. The
173+
[`Base.Order.Ordering`](@ref) abstract type provides a mechanism for defining alternate
174+
orderings on the same set of elements: when calling a sorting function like
175+
`sort!`, an instance of `Ordering` can be provided with the keyword argument `order`.
176+
177+
Instances of `Ordering` define an order through the [`Base.Order.lt`](@ref)
178+
function, which works as a generalization of `isless`.
179+
This function's behavior on custom `Ordering`s must satisfy all the conditions of a
180+
[strict weak order](https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings).
181+
See [`sort!`](@ref) for details and examples of valid and invalid `lt` functions.
184182

185183
```@docs
186184
Base.Order.Ordering

0 commit comments

Comments
 (0)