Skip to content
This repository was archived by the owner on May 28, 2024. It is now read-only.

Commit 027ca26

Browse files
committed
Add a quicksort<T: Ord>() function. Fix #11.
1 parent 95a31e8 commit 027ca26

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/quicksort.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ where F: Fn(&T, &T) -> Ordering {
6767
quicksort_helper(arr, i, right, compare);
6868
}
6969

70+
7071
/// An in-place quicksort.
7172
///
7273
/// The algorithm is from Sedgewick and Bentley, "Quicksort is Optimal":
@@ -80,20 +81,28 @@ pub fn quicksort_by<T, F>(arr: &mut [T], compare: F) where F: Fn(&T, &T) -> Orde
8081
quicksort_helper(arr, 0, (len - 1) as isize, &compare);
8182
}
8283

84+
85+
/// An in-place quicksort for ordered items.
86+
#[inline]
87+
pub fn quicksort<T>(arr: &mut [T]) where T: Ord {
88+
quicksort_by(arr, |a, b| a.cmp(b))
89+
}
90+
91+
8392
#[cfg(test)]
8493
pub mod test {
8594
use rand;
8695
use rand::Rng;
8796

88-
use super::quicksort_by;
97+
use super::quicksort;
8998

9099
#[test]
91100
pub fn random() {
92101
let mut rng = rand::thread_rng();
93102
for _ in 0u32 .. 50000u32 {
94103
let len: usize = rng.gen();
95104
let mut v: Vec<isize> = rng.gen_iter::<isize>().take((len % 32) + 1).collect();
96-
quicksort_by(&mut v, |a, b| a.cmp(b));
105+
quicksort(&mut v);
97106
for i in 0 .. v.len() - 1 {
98107
assert!(v[i] <= v[i + 1])
99108
}

0 commit comments

Comments
 (0)