@@ -14,12 +14,14 @@ Add an unstable sort to libcore.
14
14
At the moment, the only sort function we have in libstd is ` slice::sort ` . It is stable,
15
15
allocates additional memory, and is unavailable in ` #![no_std] ` environments.
16
16
17
- Stable sorting, although a good default, is very rarely useful. Users much more often
18
- value higher performance, lower memory overhead, and compatibility with ` #![no_std] ` .
17
+ The sort function is stable, which is a good but conservative default. However,
18
+ stability is rarely a required property in practice, and some other characteristics
19
+ of sort algorithms like higher performance or lower memory overhead are often more
20
+ desirable.
19
21
20
- Having a really performant, non-allocating sort function in libcore would cover these
21
- needs. At the moment, Rust is compromising on these highly regarded qualities for a
22
- systems programming language by not offering a built-in alternative .
22
+ Having a performant, non-allocating unstable sort function in libcore would cover those
23
+ needs. At the moment Rust is not offering a built-in alternative (only crates), which
24
+ is unusual for a systems programming language .
23
25
24
26
** Q: What is stability?** <br >
25
27
A: A sort function is stable if it doesn't reorder equal elements. For example:
@@ -39,8 +41,8 @@ assert!(orig == v); // MAY FAIL!
39
41
** Q: When is stability useful?** <br >
40
42
A: Not very often. A typical example is sorting columns in interactive GUI tables.
41
43
E.g. you want to have rows sorted by column X while breaking ties by column Y, so you
42
- first click on column Y and then click on column X. This is a rare case where stable
43
- sorting is important.
44
+ first click on column Y and then click on column X. This is a use case where stability
45
+ is important.
44
46
45
47
** Q: Can stable sort be performed using unstable sort?** <br >
46
48
A: Yes. If we transform ` [T] ` into ` [(T, usize)] ` by pairing every element with it's
@@ -122,7 +124,7 @@ assert!(v == [1, 2, -3, 4, -5]);
122
124
Proposed implementaton is available in the [ pdqsort] [ stjepang-pdqsort ] crate.
123
125
124
126
** Q: Why choose this particular sort algorithm?** <br >
125
- A: First, let's see what unstable sort algorithms other languages use:
127
+ A: First, let's analyse what unstable sort algorithms other languages use:
126
128
127
129
* C: quicksort
128
130
* C++: introsort
@@ -142,7 +144,7 @@ Java (talking about `Arrays.sort`, not `Collections.sort`) uses dual-pivot
142
144
quicksort. It is an improvement of quicksort that chooses two pivots for finer
143
145
grained partitioning, offering better performance in practice.
144
146
145
- A very interesting improvement of introsort is [ pattern-defeating quicksort] [ orlp-pdqsort ] ,
147
+ A recent improvement of introsort is [ pattern-defeating quicksort] [ orlp-pdqsort ] ,
146
148
which is substantially faster in common cases. One of the key tricks pdqsort
147
149
uses is block partitioning described in the [ BlockQuicksort] [ blockquicksort ] paper.
148
150
This algorithm still hasn't been built into in any programming language's
@@ -163,7 +165,7 @@ that, `slice::sort` should be generally slower than pdqsort.
163
165
** Q: What about radix sort?** <br >
164
166
A: Radix sort is usually blind to patterns in slices. It treats totally random
165
167
and partially sorted the same way. It is probably possible to improve it
166
- by combining it with other techniques until it becomes a hybrid sort . Moreover,
168
+ by combining it with some other techniques, but it's not trivial . Moreover,
167
169
radix sort is incompatible with comparison-based sorting, which makes it
168
170
an awkward choice for a general-purpose API. On top of all this, it's
169
171
not even that much faster than pdqsort anyway.
@@ -183,8 +185,8 @@ as a faster non-allocating alternative. The documentation for
183
185
# Drawbacks
184
186
[ drawbacks ] : #drawbacks
185
187
186
- The implementation of sort algorithms will grow bigger , and there will be more
187
- code to review.
188
+ The amount of code for sort algorithms will grow, and there will be more code
189
+ to review.
188
190
189
191
It might be surprising to discover cases where ` slice::sort ` is faster than
190
192
` slice::sort_unstable ` . However, these peculiarities can be explained in
0 commit comments