Skip to content

Commit 32c2951

Browse files
kshyattKristofferC
authored andcommitted
Doc sorting algos (#28514)
(cherry picked from commit e21c1bb)
1 parent 9dce186 commit 32c2951

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

base/sort.jl

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,20 @@ struct InsertionSortAlg <: Algorithm end
370370
struct QuickSortAlg <: Algorithm end
371371
struct MergeSortAlg <: Algorithm end
372372

373+
"""
374+
PartialQuickSort{T <: Union{Int,OrdinalRange}}
375+
376+
Indicate that a sorting function should use the partial quick sort
377+
algorithm. Partial quick sort returns the smallest `k` elements sorted from smallest
378+
to largest, finding them and sorting them using [`QuickSort`](@ref).
379+
380+
Characteristics:
381+
* *not stable*: does not preserve the ordering of elements which
382+
compare equal (e.g. "a" and "A" in a sort of letters which
383+
ignores case).
384+
* *in-place* in memory.
385+
* *divide-and-conquer*: sort strategy similar to [`MergeSort`](@ref).
386+
"""
373387
struct PartialQuickSort{T <: Union{Int,OrdinalRange}} <: Algorithm
374388
k::T
375389
end
@@ -379,8 +393,54 @@ Base.last(a::PartialQuickSort{Int}) = a.k
379393
Base.first(a::PartialQuickSort) = first(a.k)
380394
Base.last(a::PartialQuickSort) = last(a.k)
381395

396+
"""
397+
InsertionSort
398+
399+
Indicate that a sorting function should use the insertion sort
400+
algorithm. Insertion sort traverses the collection one element
401+
at a time, inserting each element into its correct, sorted position in
402+
the output list.
403+
404+
Characteristics:
405+
* *stable*: preserves the ordering of elements which
406+
compare equal (e.g. "a" and "A" in a sort of letters
407+
which ignores case).
408+
* *in-place* in memory.
409+
* *quadratic performance* in the number of elements to be sorted:
410+
it is well-suited to small collections but should not be used for large ones.
411+
"""
382412
const InsertionSort = InsertionSortAlg()
413+
"""
414+
QuickSort
415+
416+
Indicate that a sorting function should use the quick sort
417+
algorithm, which is *not* stable.
418+
419+
Characteristics:
420+
* *not stable*: does not preserve the ordering of elements which
421+
compare equal (e.g. "a" and "A" in a sort of letters which
422+
ignores case).
423+
* *in-place* in memory.
424+
* *divide-and-conquer*: sort strategy similar to [`MergeSort`](@ref).
425+
* *good performance* for large collections.
426+
"""
383427
const QuickSort = QuickSortAlg()
428+
"""
429+
MergeSort
430+
431+
Indicate that a sorting function should use the merge sort
432+
algorithm. Merge sort divides the collection into
433+
subcollections and repeatedly merges them, sorting each
434+
subcollection at each step, until the entire
435+
collection has been recombined in sorted form.
436+
437+
Characteristics:
438+
* *stable*: preserves the ordering of elements which compare
439+
equal (e.g. "a" and "A" in a sort of letters which ignores
440+
case).
441+
* *not in-place* in memory.
442+
* *divide-and-conquer* sort strategy.
443+
"""
384444
const MergeSort = MergeSortAlg()
385445

386446
const DEFAULT_UNSTABLE = QuickSort

doc/src/base/sort.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ can be specified via the `lt` keyword.
110110
Base.sort!
111111
Base.sort
112112
Base.sortperm
113+
Base.InsertionSort
114+
Base.MergeSort
115+
Base.QuickSort
116+
Base.PartialQuickSort
113117
Base.Sort.sortperm!
114118
Base.Sort.sortslices
115119
```
@@ -131,10 +135,10 @@ Base.Sort.partialsortperm!
131135

132136
There are currently four sorting algorithms available in base Julia:
133137

134-
* `InsertionSort`
135-
* `QuickSort`
136-
* `PartialQuickSort(k)`
137-
* `MergeSort`
138+
* [`InsertionSort`](@ref)
139+
* [`QuickSort`](@ref)
140+
* [`PartialQuickSort(k)`](@ref)
141+
* [`MergeSort`](@ref)
138142

139143
`InsertionSort` is an O(n^2) stable sorting algorithm. It is efficient for very small `n`, and
140144
is used internally by `QuickSort`.

0 commit comments

Comments
 (0)