Skip to content

Add quick_sort3 function based on 'QuicksortIsOptimal.pdf' (see comments #402

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

Closed
wants to merge 2 commits 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
89 changes: 82 additions & 7 deletions src/lib/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import vec::slice;
export lteq;
export merge_sort;
export quick_sort;
export quick_sort3;

type lteq[T] = fn(&T a, &T b) -> bool;

Expand Down Expand Up @@ -52,18 +53,12 @@ fn swap[T](vec[mutable T] arr, uint x, uint y) {
fn part[T](lteq[T] compare_func, vec[mutable T] arr, uint left,
uint right, uint pivot) -> uint {

fn compare[T](lteq[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)) {
if (compare_func(arr.(i), pivot_value)) {
swap[T](arr, i, storage_index);
storage_index += 1u;
}
Expand Down Expand Up @@ -95,6 +90,86 @@ fn quick_sort[T](lteq[T] compare_func, vec[mutable T] arr) {
qsort[T](compare_func, arr, 0u, (len[T](arr)) - 1u);
}


// 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'

fn qsort3[T](lteq[T] compare_func_lt, lteq[T] compare_func_eq,
vec[mutable T] arr, int left, int right) {

if (right <= left) {
ret;
}

let T v = arr.(right);
let int i = left - 1;
let int j = right;
let int p = i;
let int q = j;

while (true) {
i += 1;
while (compare_func_lt(arr.(i), v)) {
i += 1;
}
j -= 1;
while (compare_func_lt(v, arr.(j))) {
if (j == left) {
break;
}
j -= 1;
}
if (i >= j) {
break;
}
swap[T](arr, i as uint, j as uint);
if (compare_func_eq(arr.(i), v)) {
p += 1;
swap[T](arr, p as uint, i as uint);
}
if (compare_func_eq(v, arr.(j))) {
q -= 1;
swap[T](arr, j as uint, q as uint);
}
}
swap[T](arr, i as uint, right as uint);
j = i - 1;
i += 1;

let int k = left;
while (k < p) {
swap[T](arr, k as uint, j as uint);
k += 1;
j -= 1;
if (k == vec::len[T](arr) as int) {
break;
}
}
k = right - 1;
while (k > q) {
swap[T](arr, i as uint, k as uint);
k -= 1;
i += 1;
if (k == 0) {
break;
}
}

qsort3[T](compare_func_lt, compare_func_eq, arr, left, j);
qsort3[T](compare_func_lt, compare_func_eq, arr, i, right);
}

fn quick_sort3[T](lteq[T] compare_func_lt, lteq[T] compare_func_eq,
vec[mutable T] arr) {
if (vec::len[T](arr) == 0u) {
ret;
}
qsort3[T](compare_func_lt, compare_func_eq, arr, 0,
(vec::len[T](arr) as int) - 1);
}

// Local Variables:
// mode: rust;
// fill-column: 78;
Expand Down
66 changes: 66 additions & 0 deletions src/test/run-pass/lib-qsort3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std;

fn check_sort(vec[mutable int] v1, vec[mutable int] v2) {
auto len = std::vec::len[int](v1);

fn lt(&int a, &int b) -> bool {
ret a < b;
}
fn equal(&int a, &int b) -> bool {
ret a == b;
}
auto f1 = lt;
auto f2 = equal;
std::sort::quick_sort3[int](f1, f2, v1);
auto i = 0u;
while (i < len) {
log v2.(i);
assert (v2.(i) == v1.(i));
i += 1u;
}
}


fn main() {
{
auto v1 = [mutable 3,7,4,5,2,9,5,8];
auto v2 = [mutable 2,3,4,5,5,7,8,9];
check_sort(v1, v2);
}

{
auto v1 = [mutable 1,1,1];
auto v2 = [mutable 1,1,1];
check_sort(v1, v2);
}

{
let vec[mutable int] v1 = [mutable];
let vec[mutable int] v2 = [mutable];
check_sort(v1, v2);
}

{
auto v1 = [mutable 9];
auto v2 = [mutable 9];
check_sort(v1, v2);
}

{
auto v1 = [mutable 9,3,3,3,9];
auto v2 = [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: