2011-06-15 13:19:50 -05:00
|
|
|
|
2011-08-15 18:38:23 -05:00
|
|
|
import vec::len;
|
|
|
|
import vec::slice;
|
2011-08-12 00:48:08 -05:00
|
|
|
|
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;
|
2011-05-20 21:12:16 -05:00
|
|
|
|
2011-08-12 13:47:44 -05:00
|
|
|
type lteq[T] = block(&T, &T) -> bool ;
|
2010-12-21 02:44:06 -06:00
|
|
|
|
2011-08-10 11:27:11 -05:00
|
|
|
fn merge_sort[@T](le: <eq<T>, v: &[T]) -> [T] {
|
|
|
|
fn merge[@T](le: <eq<T>, a: &[T], b: &[T]) -> [T] {
|
2011-08-12 00:48:08 -05:00
|
|
|
let rs: [T] = ~[];
|
2011-07-27 07:19:39 -05:00
|
|
|
let a_len: uint = len[T](a);
|
|
|
|
let a_ix: uint = 0u;
|
|
|
|
let b_len: uint = len[T](b);
|
|
|
|
let b_ix: uint = 0u;
|
|
|
|
while a_ix < a_len && b_ix < b_len {
|
|
|
|
if le(a.(a_ix), b.(b_ix)) {
|
2011-08-12 00:48:08 -05:00
|
|
|
rs += ~[a.(a_ix)];
|
2011-03-16 16:58:02 -05:00
|
|
|
a_ix += 1u;
|
2011-08-12 00:48:08 -05:00
|
|
|
} else { rs += ~[b.(b_ix)]; b_ix += 1u; }
|
2011-03-16 16:58:02 -05:00
|
|
|
}
|
2011-06-24 10:55:02 -05:00
|
|
|
rs += slice[T](a, a_ix, a_len);
|
|
|
|
rs += slice[T](b, b_ix, b_len);
|
|
|
|
ret rs;
|
2010-12-21 02:44:06 -06:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
let v_len: uint = len[T](v);
|
|
|
|
if v_len <= 1u { ret v; }
|
|
|
|
let mid: uint = v_len / 2u;
|
2011-08-12 00:48:08 -05:00
|
|
|
let a: [T] = slice[T](v, 0u, mid);
|
|
|
|
let b: [T] = slice[T](v, mid, v_len);
|
2011-06-15 13:19:50 -05:00
|
|
|
ret merge[T](le, merge_sort[T](le, a), merge_sort[T](le, b));
|
2010-12-21 02:44:06 -06:00
|
|
|
}
|
|
|
|
|
2011-08-12 00:48:08 -05:00
|
|
|
fn swap[@T](arr: &[mutable T], x: uint, y: uint) {
|
2011-07-27 07:19:39 -05:00
|
|
|
let a = arr.(x);
|
2011-05-05 15:08:52 -05:00
|
|
|
arr.(x) = arr.(y);
|
|
|
|
arr.(y) = a;
|
|
|
|
}
|
|
|
|
|
2011-08-10 11:27:11 -05:00
|
|
|
fn part[@T](compare_func: <eq<T>, arr: &[mutable T], left: uint,
|
2011-08-12 13:47:44 -05:00
|
|
|
right: uint, pivot: uint) -> uint {
|
2011-07-27 07:19:39 -05:00
|
|
|
let pivot_value = arr.(pivot);
|
2011-05-05 15:08:52 -05:00
|
|
|
swap[T](arr, pivot, right);
|
2011-07-27 07:19:39 -05:00
|
|
|
let storage_index: uint = left;
|
|
|
|
let i: uint = left;
|
|
|
|
while i < right {
|
|
|
|
if compare_func({ arr.(i) }, pivot_value) {
|
2011-06-15 13:19:50 -05:00
|
|
|
swap[T](arr, i, storage_index);
|
|
|
|
storage_index += 1u;
|
2011-05-05 15:08:52 -05:00
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
swap[T](arr, storage_index, right);
|
|
|
|
ret storage_index;
|
|
|
|
}
|
|
|
|
|
2011-08-10 11:27:11 -05:00
|
|
|
fn qsort[@T](compare_func: <eq<T>, arr: &[mutable 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;
|
|
|
|
let new_pivot = part[T](compare_func, arr, left, right, pivot);
|
|
|
|
if new_pivot != 0u {
|
2011-07-18 17:01:47 -05:00
|
|
|
// Need to do this check before recursing due to overflow
|
|
|
|
qsort[T](compare_func, arr, left, new_pivot - 1u);
|
|
|
|
}
|
2011-05-05 15:08:52 -05:00
|
|
|
qsort[T](compare_func, arr, new_pivot + 1u, right);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-10 11:27:11 -05:00
|
|
|
fn quick_sort[@T](compare_func: <eq<T>, arr: &[mutable T]) {
|
2011-07-27 07:19:39 -05:00
|
|
|
if len[T](arr) == 0u { ret; }
|
2011-06-15 13:19:50 -05:00
|
|
|
qsort[T](compare_func, arr, 0u, len[T](arr) - 1u);
|
2011-05-05 15:08:52 -05:00
|
|
|
}
|
|
|
|
|
2011-05-24 12:12:32 -05:00
|
|
|
|
|
|
|
// Based on algorithm presented by Sedgewick and Bentley here:
|
|
|
|
// 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'
|
2011-08-10 11:27:11 -05:00
|
|
|
fn qsort3[@T](compare_func_lt: <eq<T>, compare_func_eq: <eq<T>,
|
2011-08-12 00:48:08 -05:00
|
|
|
arr: &[mutable T], left: int, right: int) {
|
2011-07-27 07:19:39 -05:00
|
|
|
if right <= left { ret; }
|
|
|
|
let v: T = arr.(right);
|
|
|
|
let i: int = left - 1;
|
|
|
|
let j: int = right;
|
|
|
|
let p: int = i;
|
|
|
|
let q: int = j;
|
|
|
|
while true {
|
2011-05-24 12:12:32 -05:00
|
|
|
i += 1;
|
2011-07-27 07:19:39 -05:00
|
|
|
while compare_func_lt({ arr.(i) }, v) { i += 1; }
|
2011-05-24 12:12:32 -05:00
|
|
|
j -= 1;
|
2011-07-27 07:19:39 -05:00
|
|
|
while compare_func_lt(v, { arr.(j) }) {
|
|
|
|
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-05-24 12:12:32 -05:00
|
|
|
swap[T](arr, i as uint, j as uint);
|
2011-07-27 07:19:39 -05:00
|
|
|
if compare_func_eq({ arr.(i) }, v) {
|
2011-05-24 12:12:32 -05:00
|
|
|
p += 1;
|
|
|
|
swap[T](arr, p as uint, i as uint);
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
if compare_func_eq(v, { arr.(j) }) {
|
2011-05-24 12:12:32 -05:00
|
|
|
q -= 1;
|
|
|
|
swap[T](arr, j as uint, q as uint);
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
}
|
2011-05-24 12:12:32 -05:00
|
|
|
swap[T](arr, i as uint, right as uint);
|
|
|
|
j = i - 1;
|
|
|
|
i += 1;
|
2011-07-27 07:19:39 -05:00
|
|
|
let k: int = left;
|
|
|
|
while k < p {
|
2011-05-24 12:12:32 -05:00
|
|
|
swap[T](arr, k as uint, j as uint);
|
|
|
|
k += 1;
|
|
|
|
j -= 1;
|
2011-08-12 00:48:08 -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-05-24 12:12:32 -05:00
|
|
|
swap[T](arr, i as uint, k as uint);
|
|
|
|
k -= 1;
|
|
|
|
i += 1;
|
2011-07-27 07:19:39 -05:00
|
|
|
if k == 0 { break; }
|
2011-05-24 12:12:32 -05:00
|
|
|
}
|
|
|
|
qsort3[T](compare_func_lt, compare_func_eq, arr, left, j);
|
|
|
|
qsort3[T](compare_func_lt, compare_func_eq, arr, i, right);
|
|
|
|
}
|
|
|
|
|
2011-08-10 11:27:11 -05:00
|
|
|
fn quick_sort3[@T](compare_func_lt: <eq<T>, compare_func_eq: <eq<T>,
|
2011-08-12 00:48:08 -05:00
|
|
|
arr: &[mutable T]) {
|
|
|
|
if len[T](arr) == 0u { ret; }
|
2011-05-24 12:12:32 -05:00
|
|
|
qsort3[T](compare_func_lt, compare_func_eq, arr, 0,
|
2011-08-12 00:48:08 -05:00
|
|
|
(len[T](arr) as int) - 1);
|
2011-06-30 21:09:30 -05: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
|
2011-06-15 14:01:19 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-12-21 02:44:06 -06:00
|
|
|
// End:
|