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

Implement feature sort_unstable #40601

Merged
merged 9 commits into from Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tweak the constants a bit
  • Loading branch information
Stjepan Glavina committed Mar 21, 2017
commit c4454a5507be95da969712ac0326055635efe778
4 changes: 2 additions & 2 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1668,9 +1668,9 @@ fn merge_sort<T, F>(v: &mut [T], mut is_less: F)
where F: FnMut(&T, &T) -> bool
{
// Slices of up to this length get sorted using insertion sort.
const MAX_INSERTION: usize = 16;
const MAX_INSERTION: usize = 20;
// Very short runs are extended using insertion sort to span at least this many elements.
const MIN_RUN: usize = 8;
const MIN_RUN: usize = 10;

// Sorting has no meaningful behavior on zero-sized types.
if size_of::<T>() == 0 {
Expand Down
10 changes: 5 additions & 5 deletions src/libcore/slice/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ fn partition<T, F>(v: &mut [T], pivot: usize, is_less: &mut F) -> (usize, bool)
l += 1;
}

// Find the last element lesser that the pivot.
// Find the last element smaller that the pivot.
while l < r && !is_less(v.get_unchecked(r - 1), pivot) {
r -= 1;
}
Expand Down Expand Up @@ -472,7 +472,7 @@ fn choose_pivot<T, F>(v: &mut [T], is_less: &mut F) -> (usize, bool)
{
// Minimal length to choose the median-of-medians method.
// Shorter slices use the simple median-of-three method.
const SHORTEST_MEDIAN_OF_MEDIANS: usize = 90;
const SHORTEST_MEDIAN_OF_MEDIANS: usize = 80;
// Maximal number of swaps that can be performed in this function.
const MAX_SWAPS: usize = 4 * 3;

Expand Down Expand Up @@ -539,7 +539,7 @@ fn recurse<'a, T, F>(mut v: &'a mut [T], is_less: &mut F, mut pred: Option<&'a T
where F: FnMut(&T, &T) -> bool
{
// Slices of up to this length get sorted using insertion sort.
const MAX_INSERTION: usize = 16;
const MAX_INSERTION: usize = 20;

// True if the last partitioning was reasonably balanced.
let mut was_balanced = true;
Expand Down Expand Up @@ -627,8 +627,8 @@ pub fn quicksort<T, F>(v: &mut [T], mut is_less: F)
return;
}

// Limit the number of imbalanced partitions to `floor(log2(len)) + 2`.
let limit = mem::size_of::<usize>() * 8 - v.len().leading_zeros() as usize + 1;
// Limit the number of imbalanced partitions to `floor(log2(len)) + 1`.
let limit = mem::size_of::<usize>() * 8 - v.len().leading_zeros() as usize;

recurse(v, &mut is_less, None, limit);
}