rust/src/libstd/sort.rs

167 lines
4.5 KiB
Rust
Raw Normal View History

2011-10-26 18:24:31 -05:00
/*
Module: sort
2011-10-26 18:24:31 -05:00
Sorting methods
*/
2011-09-12 18:13:28 -05:00
import vec::{len, slice};
2011-08-12 00:48:08 -05:00
export merge_sort;
export quick_sort;
export quick_sort3;
2011-10-26 18:24:31 -05:00
/* Type: lteq */
type lteq<T> = block(T, T) -> bool;
2010-12-21 02:44:06 -06:00
2011-10-26 18:24:31 -05:00
/*
Function: merge_sort
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: lteq<T>, v: [const T]) -> [T] {
fn merge<T: copy>(le: lteq<T>, a: [T], b: [T]) -> [T] {
let rs: [T] = [];
let a_len: uint = len::<T>(a);
2011-07-27 07:19:39 -05:00
let a_ix: uint = 0u;
let b_len: uint = len::<T>(b);
2011-07-27 07:19:39 -05:00
let b_ix: uint = 0u;
while a_ix < a_len && b_ix < b_len {
if le(a[a_ix], b[b_ix]) {
rs += [a[a_ix]];
a_ix += 1u;
} else { rs += [b[b_ix]]; b_ix += 1u; }
}
rs += slice::<T>(a, a_ix, a_len);
rs += slice::<T>(b, b_ix, b_len);
2011-06-24 10:55:02 -05:00
ret rs;
2010-12-21 02:44:06 -06:00
}
let v_len: uint = len::<T>(v);
if v_len == 0u { ret []; }
if v_len == 1u { ret [v[0]]; }
2011-07-27 07:19:39 -05:00
let mid: uint = v_len / 2u;
let a: [T] = slice::<T>(v, 0u, mid);
let b: [T] = slice::<T>(v, mid, v_len);
ret merge::<T>(le, merge_sort::<T>(le, a), merge_sort::<T>(le, b));
2010-12-21 02:44:06 -06:00
}
fn part<T: copy>(compare_func: lteq<T>, arr: [mutable T], left: uint,
right: uint, pivot: uint) -> uint {
let pivot_value = arr[pivot];
arr[pivot] <-> arr[right];
2011-07-27 07:19:39 -05:00
let storage_index: uint = left;
let i: uint = left;
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: lteq<T>, arr: [mutable 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);
}
}
2011-10-26 18:24:31 -05:00
/*
Function: quick_sort
Quicksort. Sorts a mutable 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: lteq<T>, arr: [mutable T]) {
if len::<T>(arr) == 0u { ret; }
qsort::<T>(compare_func, arr, 0u, len::<T>(arr) - 1u);
}
fn qsort3<T: copy>(compare_func_lt: lteq<T>, compare_func_eq: lteq<T>,
arr: [mutable T], left: int, right: int) {
2011-07-27 07:19:39 -05:00
if right <= left { ret; }
let v: T = arr[right];
2011-07-27 07:19:39 -05:00
let i: int = left - 1;
let j: int = right;
let p: int = i;
let q: int = j;
while true {
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;
2011-07-27 07:19:39 -05:00
let k: int = left;
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);
}
2011-10-26 18:24:31 -05:00
// FIXME: This should take lt and eq types
/*
Function: quick_sort3
Fancy quicksort. Sorts a mutable 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>(compare_func_lt: lteq<T>, compare_func_eq: lteq<T>,
arr: [mutable T]) {
if len::<T>(arr) == 0u { ret; }
qsort3::<T>(compare_func_lt, compare_func_eq, arr, 0,
(len::<T>(arr) as int) - 1);
}
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: