Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wording around sort guarantees #38961

Merged
merged 4 commits into from Jan 26, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 36 additions & 11 deletions src/libcollections/slice.rs
Expand Up @@ -1064,8 +1064,17 @@ impl<T> [T] {

/// This is equivalent to `self.sort_by(|a, b| a.cmp(b))`.
///
/// This sort is stable and `O(n log n)` worst-case, but allocates
/// temporary storage half the size of `self`.
/// This sort is stable (i.e. does not reorder equal elements) and `O(n log n)` worst-case.
///
/// # Current implementation
///
/// The current algorithm is an adaptive, iterative merge sort inspired by
/// [timsort](https://en.wikipedia.org/wiki/Timsort).
/// It is designed to be very fast in cases where the slice is nearly sorted, or consists of
/// two or more sorted sequences concatenated one after another.
///
/// Also, it allocates temporary storage half the size of `self`, but for short slices a
/// non-allocating insertion sort is used instead.
///
/// # Examples
///
Expand All @@ -1083,11 +1092,19 @@ impl<T> [T] {
self.sort_by(|a, b| a.cmp(b))
}

/// Sorts the slice, in place, using `f` to extract a key by which to
/// order the sort by.
/// Sorts the slice using `f` to extract a key to compare elements by.
///
/// This sort is stable (i.e. does not reorder equal elements) and `O(n log n)` worst-case.
///
/// # Current implementation
///
/// This sort is stable and `O(n log n)` worst-case, but allocates
/// temporary storage half the size of `self`.
/// The current algorithm is an adaptive, iterative merge sort inspired by
/// [timsort](https://en.wikipedia.org/wiki/Timsort).
/// It is designed to be very fast in cases where the slice is nearly sorted, or consists of
/// two or more sorted sequences concatenated one after another.
///
/// Also, it allocates temporary storage half the size of `self`, but for short slices a
/// non-allocating insertion sort is used instead.
///
/// # Examples
///
Expand All @@ -1105,11 +1122,19 @@ impl<T> [T] {
self.sort_by(|a, b| f(a).cmp(&f(b)))
}

/// Sorts the slice, in place, using `compare` to compare
/// elements.
/// Sorts the slice using `compare` to compare elements.
///
/// This sort is stable (i.e. does not reorder equal elements) and `O(n log n)` worst-case.
///
/// # Current implementation
///
/// The current algorithm is an adaptive, iterative merge sort inspired by
/// [timsort](https://en.wikipedia.org/wiki/Timsort).
/// It is designed to be very fast in cases where the slice is nearly sorted, or consists of
/// two or more sorted sequences concatenated one after another.
///
/// This sort is stable and `O(n log n)` worst-case, but allocates
/// temporary storage half the size of `self`.
/// Also, it allocates temporary storage half the size of `self`, but for short slices a
/// non-allocating insertion sort is used instead.
///
/// # Examples
///
Expand Down Expand Up @@ -1535,7 +1560,7 @@ fn merge_sort<T, F>(v: &mut [T], mut compare: F)

// FIXME #12092: These numbers are platform-specific and need more extensive testing/tuning.
//
// If `v` has length up to `insertion_len`, simply switch to insertion sort because it is going
// If `v` has length up to `max_insertion`, simply switch to insertion sort because it is going
// to perform better than merge sort. For bigger types `T`, the threshold is smaller.
//
// Short runs are extended using insertion sort to span at least `min_run` elements, in order
Expand Down