Skip to content

Commit 18ff641

Browse files
author
Stjepan Glavina
committed
Minor fix in the merge_sort comments
There was an off-by-one error discovered by @tbelaire. So, the two invariants we are enforcing are: 1. Run lengths are decreasing. 2. Sum of lengths of any two adjacent runs is less than the length of their predecessor. This commit changes the comment to be clearer and have correct bounds on `i`.
1 parent dea7ef3 commit 18ff641

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/libcollections/slice.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1496,10 +1496,10 @@ unsafe fn merge<T, F>(v: &mut [T], mid: usize, buf: *mut T, compare: &mut F)
14961496
/// The algorithm identifies strictly descending and non-descending subsequences, which are called
14971497
/// natural runs. There is a stack of pending runs yet to be merged. Each newly found run is pushed
14981498
/// onto the stack, and then some pairs of adjacent runs are merged until these two invariants are
1499-
/// satisfied, for every `i` in `0 .. runs.len() - 2`:
1499+
/// satisfied:
15001500
///
1501-
/// 1. `runs[i].len > runs[i + 1].len`
1502-
/// 2. `runs[i].len > runs[i + 1].len + runs[i + 2].len`
1501+
/// 1. for every `i` in `1..runs.len()`: `runs[i - 1].len > runs[i].len`
1502+
/// 2. for every `i` in `2..runs.len()`: `runs[i - 2].len > runs[i - 1].len + runs[i].len`
15031503
///
15041504
/// The invariants ensure that the total running time is `O(n log n)` worst-case.
15051505
fn merge_sort<T, F>(v: &mut [T], mut compare: F)

0 commit comments

Comments
 (0)