Add quick sort function to the std lib.

This commit is contained in:
Kelly Wilson 2011-05-05 14:08:52 -06:00 committed by Graydon Hoare
parent 4445d6771d
commit 850dff486e
2 changed files with 114 additions and 0 deletions

View File

@ -39,6 +39,58 @@ fn merge_sort[T](lteq[T] le, vec[T] v) -> vec[T] {
merge_sort[T](le, b));
}
fn swap[T](vec[mutable T] arr, uint x, uint y) {
auto a = arr.(x);
arr.(x) = arr.(y);
arr.(y) = a;
}
fn part[T](lteq[mutable T] compare_func, vec[mutable T] arr, uint left,
uint right, uint pivot) -> uint {
fn compare[T](lteq[mutable T] compare_func, vec[mutable T]arr,
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;
}
fn qsort[T](lteq[mutable T] compare_func, vec[mutable T] arr, uint left,
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);
}
}
fn quick_sort[T](lteq[mutable T] compare_func, vec[mutable T] arr) {
if (len[mutable T](arr) == 0u) {
ret;
}
qsort[T](compare_func, arr, 0u, (len[mutable T](arr)) - 1u);
}
// Local Variables:
// mode: rust;
// fill-column: 78;

View File

@ -0,0 +1,62 @@
use std;
fn check_sort(vec[mutable int] v1, vec[mutable int] v2) {
auto len = std._vec.len[int](v1);
fn ltequal(&int a, &int b) -> bool {
ret a <= b;
}
auto f = ltequal;
std.sort.quick_sort[int](f, v1);
auto i = 0u;
while (i < len) {
log v2.(i);
assert (v2.(i) == v1.(i));
i += 1u;
}
}
fn main() {
{
auto v1 = vec(mutable 3,7,4,5,2,9,5,8);
auto v2 = vec(mutable 2,3,4,5,5,7,8,9);
check_sort(v1, v2);
}
{
auto v1 = vec(mutable 1,1,1);
auto v2 = vec(mutable 1,1,1);
check_sort(v1, v2);
}
{
let vec[mutable int] v1 = vec(mutable);
let vec[mutable int] v2 = vec(mutable);
check_sort(v1, v2);
}
{
auto v1 = vec(mutable 9);
auto v2 = vec(mutable 9);
check_sort(v1, v2);
}
{
auto v1 = vec(mutable 9,3,3,3,9);
auto v2 = vec(mutable 3,3,3,9,9);
check_sort(v1, v2);
}
}
// 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: