rust/src/libstd/sort.rs

311 lines
8.2 KiB
Rust
Raw Normal View History

//! Sorting methods
import vec::{len, push};
import int::{eq, ord};
2011-08-12 00:48:08 -05:00
2012-04-21 15:45:20 -05:00
export le;
export merge_sort;
export quick_sort;
export quick_sort3;
2012-02-03 04:53:00 -06:00
type le<T> = fn(T, T) -> bool;
2010-12-21 02:44:06 -06:00
/**
* Merge sort. Returns a new vector containing the sorted list.
*
* Has worst case O(n log n) performance, best case O(n), but
* is not space efficient. This is a stable sort.
*/
fn merge_sort<T: copy>(le: le<T>, v: ~[const T]) -> ~[T] {
2012-02-27 20:32:45 -06:00
type slice = (uint, uint);
ret merge_sort_(le, v, (0u, len(v)));
fn merge_sort_<T: copy>(le: le<T>, v: ~[const T], slice: slice)
-> ~[T] {
2012-02-27 20:32:45 -06:00
let begin = tuple::first(slice);
let end = tuple::second(slice);
let v_len = end - begin;
if v_len == 0u { ret ~[]; }
if v_len == 1u { ret ~[v[begin]]; }
2012-02-27 20:32:45 -06:00
let mid = v_len / 2u + begin;
let a = (begin, mid);
let b = (mid, end);
ret merge(le, merge_sort_(le, v, a), merge_sort_(le, v, b));
}
fn merge<T: copy>(le: le<T>, a: ~[T], b: ~[T]) -> ~[T] {
let mut rs = ~[];
2012-02-27 20:32:45 -06:00
vec::reserve(rs, len(a) + len(b));
let a_len = len(a);
let mut a_ix = 0u;
2012-02-27 20:32:45 -06:00
let b_len = len(b);
let mut b_ix = 0u;
2011-07-27 07:19:39 -05:00
while a_ix < a_len && b_ix < b_len {
if le(a[a_ix], b[b_ix]) {
vec::push(rs, a[a_ix]);
a_ix += 1u;
} else { vec::push(rs, b[b_ix]); b_ix += 1u; }
}
rs = vec::append(rs, vec::slice(a, a_ix, a_len));
rs = vec::append(rs, vec::slice(b, b_ix, b_len));
2011-06-24 10:55:02 -05:00
ret rs;
2010-12-21 02:44:06 -06:00
}
}
fn part<T: copy>(compare_func: le<T>, arr: ~[mut T], left: uint,
right: uint, pivot: uint) -> uint {
let pivot_value = arr[pivot];
arr[pivot] <-> arr[right];
let mut storage_index: uint = left;
let mut i: uint = left;
2011-07-27 07:19:39 -05:00
while i < right {
if compare_func(copy arr[i], pivot_value) {
arr[i] <-> arr[storage_index];
storage_index += 1u;
}
i += 1u;
}
arr[storage_index] <-> arr[right];
ret storage_index;
}
fn qsort<T: copy>(compare_func: le<T>, arr: ~[mut T], left: uint,
right: uint) {
2011-07-27 07:19:39 -05:00
if right > left {
let pivot = (left + right) / 2u;
let new_pivot = part::<T>(compare_func, arr, left, right, pivot);
2011-07-27 07:19:39 -05:00
if new_pivot != 0u {
// Need to do this check before recursing due to overflow
qsort::<T>(compare_func, arr, left, new_pivot - 1u);
}
qsort::<T>(compare_func, arr, new_pivot + 1u, right);
}
}
/**
* Quicksort. Sorts a mut vector in place.
*
* Has worst case O(n^2) performance, average case O(n log n).
* This is an unstable sort.
*/
fn quick_sort<T: copy>(compare_func: le<T>, arr: ~[mut T]) {
if len::<T>(arr) == 0u { ret; }
qsort::<T>(compare_func, arr, 0u, len::<T>(arr) - 1u);
}
2012-02-03 04:53:00 -06:00
fn qsort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
arr: ~[mut T], left: int, right: int) {
2011-07-27 07:19:39 -05:00
if right <= left { ret; }
let v: T = arr[right];
let mut i: int = left - 1;
let mut j: int = right;
let mut p: int = i;
let mut q: int = j;
loop {
i += 1;
while compare_func_lt(copy arr[i], v) { i += 1; }
j -= 1;
while compare_func_lt(v, copy arr[j]) {
2011-07-27 07:19:39 -05:00
if j == left { break; }
j -= 1;
}
2011-07-27 07:19:39 -05:00
if i >= j { break; }
arr[i] <-> arr[j];
if compare_func_eq(copy arr[i], v) {
p += 1;
arr[p] <-> arr[i];
}
if compare_func_eq(v, copy arr[j]) {
q -= 1;
arr[j] <-> arr[q];
}
}
arr[i] <-> arr[right];
j = i - 1;
i += 1;
let mut k: int = left;
2011-07-27 07:19:39 -05:00
while k < p {
arr[k] <-> arr[j];
k += 1;
j -= 1;
if k == len::<T>(arr) as int { break; }
}
k = right - 1;
2011-07-27 07:19:39 -05:00
while k > q {
arr[i] <-> arr[k];
k -= 1;
i += 1;
2011-07-27 07:19:39 -05:00
if k == 0 { break; }
}
qsort3::<T>(compare_func_lt, compare_func_eq, arr, left, j);
qsort3::<T>(compare_func_lt, compare_func_eq, arr, i, right);
}
/**
* Fancy quicksort. Sorts a mut vector in place.
*
* Based on algorithm presented by [Sedgewick and Bentley]/~
* (http://www.cs.princeton.edu/~rs/talks/QuicksortIsOptimal.pdf).
* According to these slides this is the algorithm of choice for
* 'randomly ordered keys, abstract compare' & 'small number of key values'.
*
* This is an unstable sort.
*/
fn quick_sort3<T: copy ord eq>(arr: ~[mut T]) {
if len::<T>(arr) == 0u { ret; }
2012-06-30 18:19:07 -05:00
qsort3::<T>(|x, y| x.lt(y), |x, y| x.eq(y), arr, 0,
(len::<T>(arr) as int) - 1);
}
2012-01-17 21:05:07 -06:00
#[cfg(test)]
mod test_qsort3 {
fn check_sort(v1: ~[mut int], v2: ~[mut int]) {
2012-01-17 21:05:07 -06:00
let len = vec::len::<int>(v1);
quick_sort3::<int>(v1);
let mut i = 0u;
2012-01-17 21:05:07 -06:00
while i < len {
log(debug, v2[i]);
assert (v2[i] == v1[i]);
i += 1u;
}
}
#[test]
fn test() {
{
let v1 = ~[mut 3, 7, 4, 5, 2, 9, 5, 8];
let v2 = ~[mut 2, 3, 4, 5, 5, 7, 8, 9];
2012-01-17 21:05:07 -06:00
check_sort(v1, v2);
}
{
let v1 = ~[mut 1, 1, 1];
let v2 = ~[mut 1, 1, 1];
2012-01-17 21:05:07 -06:00
check_sort(v1, v2);
}
{
let v1: ~[mut int] = ~[mut];
let v2: ~[mut int] = ~[mut];
2012-01-17 21:05:07 -06:00
check_sort(v1, v2);
}
{ let v1 = ~[mut 9]; let v2 = ~[mut 9]; check_sort(v1, v2); }
2012-01-17 21:05:07 -06:00
{
let v1 = ~[mut 9, 3, 3, 3, 9];
let v2 = ~[mut 3, 3, 3, 9, 9];
2012-01-17 21:05:07 -06:00
check_sort(v1, v2);
}
}
}
#[cfg(test)]
mod test_qsort {
fn check_sort(v1: ~[mut int], v2: ~[mut int]) {
2012-01-17 21:05:07 -06:00
let len = vec::len::<int>(v1);
2012-02-03 04:53:00 -06:00
fn leual(&&a: int, &&b: int) -> bool { ret a <= b; }
let f = leual;
2012-01-17 21:05:07 -06:00
quick_sort::<int>(f, v1);
let mut i = 0u;
2012-01-17 21:05:07 -06:00
while i < len {
log(debug, v2[i]);
assert (v2[i] == v1[i]);
i += 1u;
}
}
#[test]
fn test() {
{
let v1 = ~[mut 3, 7, 4, 5, 2, 9, 5, 8];
let v2 = ~[mut 2, 3, 4, 5, 5, 7, 8, 9];
2012-01-17 21:05:07 -06:00
check_sort(v1, v2);
}
{
let v1 = ~[mut 1, 1, 1];
let v2 = ~[mut 1, 1, 1];
2012-01-17 21:05:07 -06:00
check_sort(v1, v2);
}
{
let v1: ~[mut int] = ~[mut];
let v2: ~[mut int] = ~[mut];
2012-01-17 21:05:07 -06:00
check_sort(v1, v2);
}
{ let v1 = ~[mut 9]; let v2 = ~[mut 9]; check_sort(v1, v2); }
2012-01-17 21:05:07 -06:00
{
let v1 = ~[mut 9, 3, 3, 3, 9];
let v2 = ~[mut 3, 3, 3, 9, 9];
2012-01-17 21:05:07 -06:00
check_sort(v1, v2);
}
}
// Regression test for #750
#[test]
fn test_simple() {
let names = ~[mut 2, 1, 3];
2012-01-17 21:05:07 -06:00
let expected = ~[1, 2, 3];
2012-01-17 21:05:07 -06:00
2012-02-03 04:53:00 -06:00
fn le(&&a: int, &&b: int) -> bool { int::le(a, b) }
sort::quick_sort(le, names);
2012-01-17 21:05:07 -06:00
let immut_names = vec::from_mut(names);
let pairs = vec::zip(expected, immut_names);
2012-06-30 18:19:07 -05:00
for vec::each(pairs) |p| {
let (a, b) = p;
#debug("%d %d", a, b);
assert (a == b);
}
2012-01-17 21:05:07 -06:00
}
}
#[cfg(test)]
mod tests {
fn check_sort(v1: ~[int], v2: ~[int]) {
2012-01-17 21:05:07 -06:00
let len = vec::len::<int>(v1);
2012-02-03 04:53:00 -06:00
fn le(&&a: int, &&b: int) -> bool { ret a <= b; }
let f = le;
2012-01-17 21:05:07 -06:00
let v3 = merge_sort::<int>(f, v1);
let mut i = 0u;
2012-01-17 21:05:07 -06:00
while i < len {
log(debug, v3[i]);
assert (v3[i] == v2[i]);
i += 1u;
}
}
#[test]
fn test() {
{
let v1 = ~[3, 7, 4, 5, 2, 9, 5, 8];
let v2 = ~[2, 3, 4, 5, 5, 7, 8, 9];
2012-01-17 21:05:07 -06:00
check_sort(v1, v2);
}
{ let v1 = ~[1, 1, 1]; let v2 = ~[1, 1, 1]; check_sort(v1, v2); }
{ let v1:~[int] = ~[]; let v2:~[int] = ~[]; check_sort(v1, v2); }
{ let v1 = ~[9]; let v2 = ~[9]; check_sort(v1, v2); }
2012-01-17 21:05:07 -06:00
{
let v1 = ~[9, 3, 3, 3, 9];
let v2 = ~[3, 3, 3, 9, 9];
2012-01-17 21:05:07 -06:00
check_sort(v1, v2);
}
}
#[test]
fn test_merge_sort_mutable() {
2012-02-03 04:53:00 -06:00
fn le(&&a: int, &&b: int) -> bool { ret a <= b; }
let v1 = ~[mut 3, 2, 1];
2012-02-03 04:53:00 -06:00
let v2 = merge_sort(le, v1);
assert v2 == ~[1, 2, 3];
2012-01-17 21:05:07 -06:00
}
}
2010-12-21 02:44:06 -06:00
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End: