Skip to content

Commit

Permalink
Doc sorting algos (#28514)
Browse files Browse the repository at this point in the history
(cherry picked from commit e21c1bb)
  • Loading branch information
kshyatt authored and KristofferC committed Sep 8, 2018
1 parent b24e624 commit ff8361a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
60 changes: 60 additions & 0 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,20 @@ struct InsertionSortAlg <: Algorithm end
struct QuickSortAlg <: Algorithm end
struct MergeSortAlg <: Algorithm end

"""
PartialQuickSort{T <: Union{Int,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).
Characteristics:
* *not stable*: does not preserve the ordering of elements which
compare equal (e.g. "a" and "A" in a sort of letters which
ignores case).
* *in-place* in memory.
* *divide-and-conquer*: sort strategy similar to [`MergeSort`](@ref).
"""
struct PartialQuickSort{T <: Union{Int,OrdinalRange}} <: Algorithm
k::T
end
Expand All @@ -379,8 +393,54 @@ Base.last(a::PartialQuickSort{Int}) = a.k
Base.first(a::PartialQuickSort) = first(a.k)
Base.last(a::PartialQuickSort) = last(a.k)

"""
InsertionSort
Indicate that a sorting function should use the insertion sort
algorithm. Insertion sort traverses the collection one element
at a time, inserting each element into its correct, sorted position in
the output list.
Characteristics:
* *stable*: preserves the ordering of elements which
compare equal (e.g. "a" and "A" in a sort of letters
which ignores case).
* *in-place* in memory.
* *quadratic performance* in the number of elements to be sorted:
it is well-suited to small collections but should not be used for large ones.
"""
const InsertionSort = InsertionSortAlg()
"""
QuickSort
Indicate that a sorting function should use the quick sort
algorithm, which is *not* stable.
Characteristics:
* *not stable*: does not preserve the ordering of elements which
compare equal (e.g. "a" and "A" in a sort of letters which
ignores case).
* *in-place* in memory.
* *divide-and-conquer*: sort strategy similar to [`MergeSort`](@ref).
* *good performance* for large collections.
"""
const QuickSort = QuickSortAlg()
"""
MergeSort
Indicate that a sorting function should use the merge sort
algorithm. Merge sort divides the collection into
subcollections and repeatedly merges them, sorting each
subcollection at each step, until the entire
collection has been recombined in sorted form.
Characteristics:
* *stable*: preserves the ordering of elements which compare
equal (e.g. "a" and "A" in a sort of letters which ignores
case).
* *not in-place* in memory.
* *divide-and-conquer* sort strategy.
"""
const MergeSort = MergeSortAlg()

const DEFAULT_UNSTABLE = QuickSort
Expand Down
12 changes: 8 additions & 4 deletions doc/src/base/sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ can be specified via the `lt` keyword.
Base.sort!
Base.sort
Base.sortperm
Base.InsertionSort
Base.MergeSort
Base.QuickSort
Base.PartialQuickSort
Base.Sort.sortperm!
Base.Sort.sortslices
```
Expand All @@ -131,10 +135,10 @@ Base.Sort.partialsortperm!

There are currently four sorting algorithms available in base Julia:

* `InsertionSort`
* `QuickSort`
* `PartialQuickSort(k)`
* `MergeSort`
* [`InsertionSort`](@ref)
* [`QuickSort`](@ref)
* [`PartialQuickSort(k)`](@ref)
* [`MergeSort`](@ref)

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

0 comments on commit ff8361a

Please sign in to comment.