You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Doc: The default sorting alg. is stable from 1.9 (#47579)
* Update doc/src/base/sort.md
* Update docs: The default sorting alg. is stable
* Compat 1.9 for QuickSort to be stable
* Specify the default algorithm
* Use example from InlineStrings.jl
* Change example to jldoctest
* Remove "*appear* to be stable." as slightly misleading.
Co-authored-by: Lilith Orion Hafner <[email protected]>
The `QuickSort` and `PartialQuickSort` algorithms are stable since Julia 1.9.
176
+
169
177
`MergeSort` is an O(n log n) stable sorting algorithm but is not in-place – it requires a temporary
170
178
array of half the size of the input array – and is typically not quite as fast as `QuickSort`.
171
179
It is the default algorithm for non-numeric data.
172
180
173
-
The default sorting algorithms are chosen on the basis that they are fast and stable, or *appear*
174
-
to be so. For numeric types indeed, `QuickSort` is selected as it is faster and indistinguishable
175
-
in this case from a stable sort (unless the array records its mutations in some way). The stability
176
-
property comes at a non-negligible cost, so if you don't need it, you may want to explicitly specify
177
-
your preferred algorithm, e.g. `sort!(v, alg=QuickSort)`.
181
+
The default sorting algorithms are chosen on the basis that they are fast and stable.
182
+
Usually, `QuickSort` is selected, but `InsertionSort` is preferred for small data.
183
+
You can also explicitly specify your preferred algorithm, e.g.
184
+
`sort!(v, alg=PartialQuickSort(10:20))`.
178
185
179
-
The mechanism by which Julia picks default sorting algorithms is implemented via the `Base.Sort.defalg`
180
-
function. It allows a particular algorithm to be registered as the default in all sorting functions
181
-
for specific arrays. For example, here are the two default methods from [`sort.jl`](https://github.com/JuliaLang/julia/blob/master/base/sort.jl):
186
+
The mechanism by which Julia picks default sorting algorithms is implemented via the
187
+
`Base.Sort.defalg` function. It allows a particular algorithm to be registered as the
188
+
default in all sorting functions for specific arrays. For example, here is the default
189
+
method from [`sort.jl`](https://github.com/JuliaLang/julia/blob/master/base/sort.jl):
190
+
191
+
```julia
192
+
defalg(v::AbstractArray) = DEFAULT_STABLE
193
+
```
182
194
195
+
You may change the default behavior for specific types by defining new methods for `defalg`.
196
+
For example, [InlineStrings.jl](https://github.com/JuliaStrings/InlineStrings.jl/blob/v1.3.2/src/InlineStrings.jl#L903)
0 commit comments