2011-05-17 13:41:41 -05:00
|
|
|
import vec::len;
|
|
|
|
import vec::slice;
|
2010-12-21 02:44:06 -06:00
|
|
|
|
|
|
|
type lteq[T] = fn(&T a, &T b) -> bool;
|
|
|
|
|
|
|
|
fn merge_sort[T](lteq[T] le, vec[T] v) -> vec[T] {
|
|
|
|
|
2011-03-16 16:58:02 -05:00
|
|
|
fn merge[T](lteq[T] le, vec[T] a, vec[T] b) -> vec[T] {
|
2011-05-16 20:21:22 -05:00
|
|
|
let vec[T] res = [];
|
2011-03-16 16:58:02 -05:00
|
|
|
let uint a_len = len[T](a);
|
|
|
|
let uint a_ix = 0u;
|
|
|
|
let uint b_len = len[T](b);
|
|
|
|
let uint b_ix = 0u;
|
|
|
|
while (a_ix < a_len && b_ix < b_len) {
|
|
|
|
if (le(a.(a_ix), b.(b_ix))) {
|
2011-05-16 20:21:22 -05:00
|
|
|
res += [a.(a_ix)];
|
2011-03-16 16:58:02 -05:00
|
|
|
a_ix += 1u;
|
|
|
|
} else {
|
2011-05-16 20:21:22 -05:00
|
|
|
res += [b.(b_ix)];
|
2011-03-16 16:58:02 -05:00
|
|
|
b_ix += 1u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res += slice[T](a, a_ix, a_len);
|
|
|
|
res += slice[T](b, b_ix, b_len);
|
|
|
|
ret res;
|
2010-12-21 02:44:06 -06:00
|
|
|
}
|
|
|
|
|
2011-03-16 16:58:02 -05:00
|
|
|
let uint v_len = len[T](v);
|
2010-12-21 02:44:06 -06:00
|
|
|
|
2011-03-16 16:58:02 -05:00
|
|
|
if (v_len <= 1u) {
|
|
|
|
ret v;
|
|
|
|
}
|
2010-12-21 02:44:06 -06:00
|
|
|
|
2011-03-16 16:58:02 -05:00
|
|
|
let uint mid = v_len / 2u;
|
|
|
|
let vec[T] a = slice[T](v, 0u, mid);
|
|
|
|
let vec[T] b = 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
|
|
|
}
|
|
|
|
|
2011-05-05 15:08:52 -05:00
|
|
|
fn swap[T](vec[mutable T] arr, uint x, uint y) {
|
|
|
|
auto a = arr.(x);
|
|
|
|
arr.(x) = arr.(y);
|
|
|
|
arr.(y) = a;
|
|
|
|
}
|
|
|
|
|
2011-05-06 08:59:33 -05:00
|
|
|
fn part[T](lteq[T] compare_func, vec[mutable T] arr, uint left,
|
2011-05-05 15:08:52 -05:00
|
|
|
uint right, uint pivot) -> uint {
|
|
|
|
|
2011-05-06 08:59:33 -05:00
|
|
|
fn compare[T](lteq[T] compare_func, vec[mutable T]arr,
|
2011-05-05 15:08:52 -05:00
|
|
|
uint arr_idx, &T arr_value) -> bool {
|
|
|
|
|
|
|
|
ret compare_func(arr.(arr_idx),arr_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pivot_value = arr.(pivot);
|
|
|
|
swap[T](arr, pivot, right);
|
|
|
|
let uint storage_index = left;
|
|
|
|
let uint i = left;
|
|
|
|
while (i<right) {
|
|
|
|
if (compare[T](compare_func, arr, i, pivot_value)) {
|
|
|
|
swap[T](arr, i, storage_index);
|
|
|
|
storage_index += 1u;
|
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
swap[T](arr, storage_index, right);
|
|
|
|
ret storage_index;
|
|
|
|
}
|
|
|
|
|
2011-05-06 08:59:33 -05:00
|
|
|
fn qsort[T](lteq[T] compare_func, vec[mutable T] arr, uint left,
|
2011-05-05 15:08:52 -05:00
|
|
|
uint right) {
|
|
|
|
|
|
|
|
if (right > left) {
|
|
|
|
auto pivot = (left+right)/2u;
|
|
|
|
auto new_pivot = part[T](compare_func, arr, left, right, pivot);
|
|
|
|
if (new_pivot == 0u) {
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
qsort[T](compare_func, arr, left, new_pivot - 1u);
|
|
|
|
qsort[T](compare_func, arr, new_pivot + 1u, right);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-06 08:59:33 -05:00
|
|
|
fn quick_sort[T](lteq[T] compare_func, vec[mutable T] arr) {
|
2011-05-05 15:08:52 -05:00
|
|
|
|
2011-05-06 08:59:33 -05:00
|
|
|
if (len[T](arr) == 0u) {
|
2011-05-05 15:08:52 -05:00
|
|
|
ret;
|
|
|
|
}
|
2011-05-06 08:59:33 -05:00
|
|
|
qsort[T](compare_func, arr, 0u, (len[T](arr)) - 1u);
|
2011-05-05 15:08:52 -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
|
|
|
|
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|