Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quick sort function to the std lib. #351

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/lib/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
62 changes: 62 additions & 0 deletions src/test/run-pass/lib-qsort.rs
Original file line number Diff line number Diff line change
@@ -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: