2012-07-04 16:53:12 -05:00
|
|
|
//! Sorting methods
|
2012-08-31 18:41:15 -05:00
|
|
|
#[forbid(deprecated_mode)];
|
|
|
|
#[forbid(deprecated_pattern)];
|
|
|
|
|
2012-09-04 13:23:53 -05:00
|
|
|
use vec::{len, push};
|
|
|
|
use core::cmp::{Eq, Ord};
|
2011-08-12 00:48:08 -05:00
|
|
|
|
2012-04-21 15:45:20 -05:00
|
|
|
export le;
|
2011-05-20 21:12:16 -05:00
|
|
|
export merge_sort;
|
|
|
|
export quick_sort;
|
2011-05-24 12:12:32 -05:00
|
|
|
export quick_sort3;
|
2012-09-03 17:58:01 -05:00
|
|
|
export Sort;
|
2011-05-20 21:12:16 -05:00
|
|
|
|
2012-09-04 16:37:47 -05:00
|
|
|
type Le<T> = pure fn(v1: &T, v2: &T) -> bool;
|
2010-12-21 02:44:06 -06:00
|
|
|
|
2012-07-04 16:53:12 -05: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.
|
|
|
|
*/
|
2012-09-07 16:52:28 -05:00
|
|
|
fn merge_sort<T: Copy>(le: Le<T>, v: &[const T]) -> ~[T] {
|
2012-09-04 16:37:47 -05:00
|
|
|
type Slice = (uint, uint);
|
2012-02-27 20:32:45 -06:00
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
return merge_sort_(le, v, (0u, len(v)));
|
2012-02-27 20:32:45 -06:00
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
fn merge_sort_<T: Copy>(le: Le<T>, v: &[const T], slice: Slice)
|
2012-06-29 18:26:56 -05:00
|
|
|
-> ~[T] {
|
2012-07-16 16:32:59 -05:00
|
|
|
let begin = slice.first();
|
|
|
|
let end = slice.second();
|
2012-02-27 20:32:45 -06:00
|
|
|
|
|
|
|
let v_len = end - begin;
|
2012-08-01 19:30:05 -05:00
|
|
|
if v_len == 0u { return ~[]; }
|
|
|
|
if v_len == 1u { return ~[v[begin]]; }
|
2012-02-27 20:32:45 -06:00
|
|
|
|
|
|
|
let mid = v_len / 2u + begin;
|
|
|
|
let a = (begin, mid);
|
|
|
|
let b = (mid, end);
|
2012-08-01 19:30:05 -05:00
|
|
|
return merge(le, merge_sort_(le, v, a), merge_sort_(le, v, b));
|
2012-02-27 20:32:45 -06:00
|
|
|
}
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
fn merge<T: Copy>(le: Le<T>, a: &[T], b: &[T]) -> ~[T] {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut rs = ~[];
|
2012-02-27 20:32:45 -06:00
|
|
|
vec::reserve(rs, len(a) + len(b));
|
|
|
|
let a_len = len(a);
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut a_ix = 0u;
|
2012-02-27 20:32:45 -06:00
|
|
|
let b_len = len(b);
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut b_ix = 0u;
|
2011-07-27 07:19:39 -05:00
|
|
|
while a_ix < a_len && b_ix < b_len {
|
2012-08-02 17:42:56 -05:00
|
|
|
if le(&a[a_ix], &b[b_ix]) {
|
2012-06-25 18:22:22 -05:00
|
|
|
vec::push(rs, a[a_ix]);
|
2011-03-16 16:58:02 -05:00
|
|
|
a_ix += 1u;
|
2012-06-25 18:22:22 -05:00
|
|
|
} else { vec::push(rs, b[b_ix]); b_ix += 1u; }
|
2011-03-16 16:58:02 -05:00
|
|
|
}
|
2012-06-28 00:49:05 -05:00
|
|
|
rs = vec::append(rs, vec::slice(a, a_ix, a_len));
|
|
|
|
rs = vec::append(rs, vec::slice(b, b_ix, b_len));
|
2012-08-01 19:30:05 -05:00
|
|
|
return rs;
|
2010-12-21 02:44:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
fn part<T: Copy>(compare_func: Le<T>, arr: &[mut T], left: uint,
|
2011-11-18 05:39:20 -06:00
|
|
|
right: uint, pivot: uint) -> uint {
|
2011-08-19 17:16:48 -05:00
|
|
|
let pivot_value = arr[pivot];
|
2011-11-18 05:39:20 -06:00
|
|
|
arr[pivot] <-> arr[right];
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut storage_index: uint = left;
|
|
|
|
let mut i: uint = left;
|
2011-07-27 07:19:39 -05:00
|
|
|
while i < right {
|
2012-08-02 17:42:56 -05:00
|
|
|
if compare_func(&arr[i], &pivot_value) {
|
2011-11-18 05:39:20 -06:00
|
|
|
arr[i] <-> arr[storage_index];
|
2011-06-15 13:19:50 -05:00
|
|
|
storage_index += 1u;
|
2011-05-05 15:08:52 -05:00
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
2011-11-18 05:39:20 -06:00
|
|
|
arr[storage_index] <-> arr[right];
|
2012-08-01 19:30:05 -05:00
|
|
|
return storage_index;
|
2011-05-05 15:08:52 -05:00
|
|
|
}
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
fn qsort<T: Copy>(compare_func: Le<T>, arr: &[mut T], left: uint,
|
2011-08-12 13:47:44 -05:00
|
|
|
right: uint) {
|
2011-07-27 07:19:39 -05:00
|
|
|
if right > left {
|
|
|
|
let pivot = (left + right) / 2u;
|
2011-08-12 12:56:57 -05:00
|
|
|
let new_pivot = part::<T>(compare_func, arr, left, right, pivot);
|
2011-07-27 07:19:39 -05:00
|
|
|
if new_pivot != 0u {
|
2011-07-18 17:01:47 -05:00
|
|
|
// Need to do this check before recursing due to overflow
|
2011-08-12 12:56:57 -05:00
|
|
|
qsort::<T>(compare_func, arr, left, new_pivot - 1u);
|
2011-07-18 17:01:47 -05:00
|
|
|
}
|
2011-08-12 12:56:57 -05:00
|
|
|
qsort::<T>(compare_func, arr, new_pivot + 1u, right);
|
2011-05-05 15:08:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-09-07 16:52:28 -05:00
|
|
|
fn quick_sort<T: Copy>(compare_func: Le<T>, arr: &[mut T]) {
|
2012-08-01 19:30:05 -05:00
|
|
|
if len::<T>(arr) == 0u { return; }
|
2011-08-12 12:56:57 -05:00
|
|
|
qsort::<T>(compare_func, arr, 0u, len::<T>(arr) - 1u);
|
2011-05-05 15:08:52 -05:00
|
|
|
}
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
fn qsort3<T: Copy Ord Eq>(arr: &[mut T], left: int, right: int) {
|
2012-08-01 19:30:05 -05:00
|
|
|
if right <= left { return; }
|
2011-08-19 17:16:48 -05:00
|
|
|
let v: T = arr[right];
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut i: int = left - 1;
|
|
|
|
let mut j: int = right;
|
|
|
|
let mut p: int = i;
|
|
|
|
let mut q: int = j;
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2011-05-24 12:12:32 -05:00
|
|
|
i += 1;
|
2012-08-27 23:17:42 -05:00
|
|
|
while arr[i] < v { i += 1; }
|
2011-05-24 12:12:32 -05:00
|
|
|
j -= 1;
|
2012-08-27 23:17:42 -05:00
|
|
|
while v < arr[j] {
|
2011-07-27 07:19:39 -05:00
|
|
|
if j == left { break; }
|
2011-05-24 12:12:32 -05:00
|
|
|
j -= 1;
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
if i >= j { break; }
|
2011-11-18 05:39:20 -06:00
|
|
|
arr[i] <-> arr[j];
|
2012-08-27 23:17:42 -05:00
|
|
|
if arr[i] == v {
|
2011-05-24 12:12:32 -05:00
|
|
|
p += 1;
|
2011-11-18 05:39:20 -06:00
|
|
|
arr[p] <-> arr[i];
|
2011-05-24 12:12:32 -05:00
|
|
|
}
|
2012-08-27 23:17:42 -05:00
|
|
|
if v == arr[j] {
|
2011-05-24 12:12:32 -05:00
|
|
|
q -= 1;
|
2011-11-18 05:39:20 -06:00
|
|
|
arr[j] <-> arr[q];
|
2011-05-24 12:12:32 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
2011-11-18 05:39:20 -06:00
|
|
|
arr[i] <-> arr[right];
|
2011-05-24 12:12:32 -05:00
|
|
|
j = i - 1;
|
|
|
|
i += 1;
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut k: int = left;
|
2011-07-27 07:19:39 -05:00
|
|
|
while k < p {
|
2011-11-18 05:39:20 -06:00
|
|
|
arr[k] <-> arr[j];
|
2011-05-24 12:12:32 -05:00
|
|
|
k += 1;
|
|
|
|
j -= 1;
|
2011-08-12 12:56:57 -05:00
|
|
|
if k == len::<T>(arr) as int { break; }
|
2011-05-24 12:12:32 -05:00
|
|
|
}
|
|
|
|
k = right - 1;
|
2011-07-27 07:19:39 -05:00
|
|
|
while k > q {
|
2011-11-18 05:39:20 -06:00
|
|
|
arr[i] <-> arr[k];
|
2011-05-24 12:12:32 -05:00
|
|
|
k -= 1;
|
|
|
|
i += 1;
|
2011-07-27 07:19:39 -05:00
|
|
|
if k == 0 { break; }
|
2011-05-24 12:12:32 -05:00
|
|
|
}
|
2012-08-27 23:17:42 -05:00
|
|
|
qsort3::<T>(arr, left, j);
|
|
|
|
qsort3::<T>(arr, i, right);
|
2011-05-24 12:12:32 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Fancy quicksort. Sorts a mut vector in place.
|
|
|
|
*
|
2012-07-11 18:49:02 -05:00
|
|
|
* Based on algorithm presented by ~[Sedgewick and Bentley]
|
2012-07-04 16:53:12 -05:00
|
|
|
* (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.
|
|
|
|
*/
|
2012-09-07 16:52:28 -05:00
|
|
|
fn quick_sort3<T: Copy Ord Eq>(arr: &[mut T]) {
|
2012-08-02 17:42:56 -05:00
|
|
|
if arr.len() <= 1 { return; }
|
2012-08-27 23:17:42 -05:00
|
|
|
qsort3(arr, 0, (arr.len() - 1) as int);
|
2011-06-30 21:09:30 -05:00
|
|
|
}
|
|
|
|
|
2012-09-03 17:58:01 -05:00
|
|
|
trait Sort {
|
|
|
|
fn qsort(self);
|
|
|
|
}
|
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
impl<T: Copy Ord Eq> &[mut T] : Sort {
|
2012-09-03 17:58:01 -05:00
|
|
|
fn qsort(self) { quick_sort3(self); }
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test_qsort3 {
|
2012-09-21 20:10:45 -05:00
|
|
|
#[legacy_exports];
|
2012-08-31 18:41:15 -05:00
|
|
|
fn check_sort(v1: &[mut int], v2: &[mut int]) {
|
2012-01-17 21:05:07 -06:00
|
|
|
let len = vec::len::<int>(v1);
|
2012-06-06 16:19:52 -05:00
|
|
|
quick_sort3::<int>(v1);
|
2012-03-22 10:39:41 -05:00
|
|
|
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() {
|
|
|
|
{
|
2012-06-29 18:26:56 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
{
|
2012-06-29 18:26:56 -05:00
|
|
|
let v1 = ~[mut 1, 1, 1];
|
|
|
|
let v2 = ~[mut 1, 1, 1];
|
2012-01-17 21:05:07 -06:00
|
|
|
check_sort(v1, v2);
|
|
|
|
}
|
|
|
|
{
|
2012-06-29 18:26:56 -05:00
|
|
|
let v1: ~[mut int] = ~[mut];
|
|
|
|
let v2: ~[mut int] = ~[mut];
|
2012-01-17 21:05:07 -06:00
|
|
|
check_sort(v1, v2);
|
|
|
|
}
|
2012-06-29 18:26:56 -05:00
|
|
|
{ let v1 = ~[mut 9]; let v2 = ~[mut 9]; check_sort(v1, v2); }
|
2012-01-17 21:05:07 -06:00
|
|
|
{
|
2012-06-29 18:26:56 -05: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 {
|
2012-09-21 20:10:45 -05:00
|
|
|
#[legacy_exports];
|
2012-08-31 18:41:15 -05:00
|
|
|
fn check_sort(v1: &[mut int], v2: &[mut int]) {
|
2012-01-17 21:05:07 -06:00
|
|
|
let len = vec::len::<int>(v1);
|
2012-08-02 17:42:56 -05:00
|
|
|
pure fn leual(a: &int, b: &int) -> bool { *a <= *b }
|
|
|
|
quick_sort::<int>(leual, v1);
|
2012-03-22 10:39:41 -05:00
|
|
|
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() {
|
|
|
|
{
|
2012-06-29 18:26:56 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
{
|
2012-06-29 18:26:56 -05:00
|
|
|
let v1 = ~[mut 1, 1, 1];
|
|
|
|
let v2 = ~[mut 1, 1, 1];
|
2012-01-17 21:05:07 -06:00
|
|
|
check_sort(v1, v2);
|
|
|
|
}
|
|
|
|
{
|
2012-06-29 18:26:56 -05:00
|
|
|
let v1: ~[mut int] = ~[mut];
|
|
|
|
let v2: ~[mut int] = ~[mut];
|
2012-01-17 21:05:07 -06:00
|
|
|
check_sort(v1, v2);
|
|
|
|
}
|
2012-06-29 18:26:56 -05:00
|
|
|
{ let v1 = ~[mut 9]; let v2 = ~[mut 9]; check_sort(v1, v2); }
|
2012-01-17 21:05:07 -06:00
|
|
|
{
|
2012-06-29 18:26:56 -05: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() {
|
2012-06-29 18:26:56 -05:00
|
|
|
let names = ~[mut 2, 1, 3];
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
let expected = ~[1, 2, 3];
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-08-30 14:54:50 -05:00
|
|
|
sort::quick_sort(|x, y| { int::le(*x, *y) }, names);
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
let immut_names = vec::from_mut(names);
|
|
|
|
|
|
|
|
let pairs = vec::zip(expected, immut_names);
|
2012-09-18 23:41:13 -05:00
|
|
|
for vec::each(pairs) |p| {
|
2012-09-18 23:41:37 -05:00
|
|
|
let (a, b) = *p;
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("%d %d", a, b);
|
2012-03-27 08:14:12 -05:00
|
|
|
assert (a == b);
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2012-09-21 20:10:45 -05:00
|
|
|
#[legacy_exports];
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-08-31 18:41:15 -05:00
|
|
|
fn check_sort(v1: &[int], v2: &[int]) {
|
2012-01-17 21:05:07 -06:00
|
|
|
let len = vec::len::<int>(v1);
|
2012-08-02 17:42:56 -05:00
|
|
|
pure fn le(a: &int, b: &int) -> bool { *a <= *b }
|
2012-02-03 04:53:00 -06:00
|
|
|
let f = le;
|
2012-01-17 21:05:07 -06:00
|
|
|
let v3 = merge_sort::<int>(f, v1);
|
2012-03-22 10:39:41 -05:00
|
|
|
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() {
|
|
|
|
{
|
2012-06-29 18:26:56 -05:00
|
|
|
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);
|
|
|
|
}
|
2012-06-29 18:26:56 -05:00
|
|
|
{ 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
|
|
|
{
|
2012-06-29 18:26:56 -05: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-08-02 17:42:56 -05:00
|
|
|
pure fn le(a: &int, b: &int) -> bool { *a <= *b }
|
2012-06-29 18:26:56 -05:00
|
|
|
let v1 = ~[mut 3, 2, 1];
|
2012-02-03 04:53:00 -06:00
|
|
|
let v2 = merge_sort(le, v1);
|
2012-06-29 18:26:56 -05:00
|
|
|
assert v2 == ~[1, 2, 3];
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2012-09-06 14:11:48 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_merge_sort_stability()
|
|
|
|
{
|
|
|
|
// tjc: funny that we have to use parens
|
|
|
|
pure fn ile(x: &(&static/str), y: &(&static/str)) -> bool
|
|
|
|
{
|
2012-09-18 13:17:40 -05:00
|
|
|
unsafe // to_lower is not pure...
|
2012-09-06 14:11:48 -05:00
|
|
|
{
|
|
|
|
let x = x.to_lower();
|
|
|
|
let y = y.to_lower();
|
|
|
|
x <= y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let names1 = ~["joe bob", "Joe Bob", "Jack Brown", "JOE Bob",
|
|
|
|
"Sally Mae", "JOE BOB", "Alex Andy"];
|
|
|
|
let names2 = ~["Alex Andy", "Jack Brown", "joe bob", "Joe Bob",
|
|
|
|
"JOE Bob", "JOE BOB", "Sally Mae"];
|
|
|
|
let names3 = merge_sort(ile, names1);
|
|
|
|
assert names3 == names2;
|
|
|
|
}
|
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:
|