@@ -21,7 +21,8 @@ julia> sort([2,3,1], rev=true)
21
21
1
22
22
```
23
23
24
- To sort an array in-place, use the "bang" version of the sort function:
24
+ ` sort ` constructs a sorted copy leaving its input unchanged. Use the "bang" version of
25
+ the sort function to mutate an existing array:
25
26
26
27
``` jldoctest
27
28
julia> a = [2,3,1];
@@ -134,74 +135,33 @@ Base.Sort.partialsortperm!
134
135
135
136
## Sorting Algorithms
136
137
137
- There are currently four sorting algorithms available in base Julia:
138
+ There are currently four sorting algorithms publicly available in base Julia:
138
139
139
140
* [ ` InsertionSort ` ] ( @ref )
140
141
* [ ` QuickSort ` ] ( @ref )
141
142
* [ ` PartialQuickSort(k) ` ] ( @ref )
142
143
* [ ` MergeSort ` ] ( @ref )
143
144
144
- ` InsertionSort ` is an O(n²) stable sorting algorithm. It is efficient for very small ` n ` ,
145
- and is used internally by ` QuickSort ` .
145
+ By default, the ` sort ` family of functions uses stable sorting algorithms that are fast
146
+ on most inputs. The exact algorithm choice is an implementation detail to allow for
147
+ future performance improvements. Currently, a hybrid of ` RadixSort ` , ` ScratchQuickSort ` ,
148
+ ` InsertionSort ` , and ` CountingSort ` is used based on input type, size, and composition.
149
+ Implementation details are subject to change but currently available in the extended help
150
+ of ` ??Base.DEFAULT_STABLE ` and the docstrings of internal sorting algorithms listed there.
146
151
147
- ` QuickSort ` is a very fast sorting algorithm with an average-case time complexity of
148
- O(n log n). ` QuickSort ` is stable, i.e., elements considered equal will remain in the same
149
- order. Notice that O(n²) is worst-case complexity, but it gets vanishingly unlikely as the
150
- pivot selection is randomized.
151
-
152
- ` PartialQuickSort(k::OrdinalRange) ` is similar to ` QuickSort ` , but the output array is only
153
- sorted in the range of ` k ` . For example:
154
-
155
- ``` jldoctest
156
- julia> x = rand(1:500, 100);
157
-
158
- julia> k = 50:100;
159
-
160
- julia> s1 = sort(x; alg=QuickSort);
161
-
162
- julia> s2 = sort(x; alg=PartialQuickSort(k));
163
-
164
- julia> map(issorted, (s1, s2))
165
- (true, false)
166
-
167
- julia> map(x->issorted(x[k]), (s1, s2))
168
- (true, true)
169
-
170
- julia> s1[k] == s2[k]
171
- true
172
- ```
173
-
174
- !!! compat "Julia 1.9"
175
- The ` QuickSort ` and ` PartialQuickSort ` algorithms are stable since Julia 1.9.
176
-
177
- ` MergeSort ` is an O(n log n) stable sorting algorithm but is not in-place – it requires a temporary
178
- array of half the size of the input array – and is typically not quite as fast as ` QuickSort ` .
179
- It is the default algorithm for non-numeric data.
180
-
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)) ` .
185
-
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
- ```
194
-
195
- You may change the default behavior for specific types by defining new methods for ` defalg ` .
152
+ You can explicitly specify your preferred algorithm with the ` alg ` keyword
153
+ (e.g. ` sort!(v, alg=PartialQuickSort(10:20)) ` ) or reconfigure the default sorting algorithm
154
+ for custom types by adding a specialized method to the ` Base.Sort.defalg ` function.
196
155
For example, [ InlineStrings.jl] ( https://github.com/JuliaStrings/InlineStrings.jl/blob/v1.3.2/src/InlineStrings.jl#L903 )
197
156
defines the following method:
198
157
``` julia
199
158
Base. Sort. defalg (:: AbstractArray{<:Union{SmallInlineStrings, Missing}} ) = InlineStringSort
200
159
```
201
160
202
161
!!! compat "Julia 1.9"
203
- The default sorting algorithm (returned by ` Base.Sort.defalg ` ) is guaranteed
204
- to be stable since Julia 1.9. Previous versions had unstable edge cases when sorting numeric arrays.
162
+ The default sorting algorithm (returned by ` Base.Sort.defalg ` ) is guaranteed to
163
+ be stable since Julia 1.9. Previous versions had unstable edge cases when
164
+ sorting numeric arrays.
205
165
206
166
## Alternate orderings
207
167
0 commit comments