Skip to content

Commit fbc4f88

Browse files
wilsonkgraydon
authored andcommitted
---
yaml --- r: 2393 b: refs/heads/master c: 850dff4 h: refs/heads/master i: 2391: d1c0356 v: v3
1 parent 4ea1a0a commit fbc4f88

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 4445d6771d68d17e14e491475c74492b7d2e1205
2+
refs/heads/master: 850dff486eb2605ad6e3c9434e3a66869dbdb943

trunk/src/lib/sort.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,58 @@ fn merge_sort[T](lteq[T] le, vec[T] v) -> vec[T] {
3939
merge_sort[T](le, b));
4040
}
4141

42+
fn swap[T](vec[mutable T] arr, uint x, uint y) {
43+
auto a = arr.(x);
44+
arr.(x) = arr.(y);
45+
arr.(y) = a;
46+
}
47+
48+
fn part[T](lteq[mutable T] compare_func, vec[mutable T] arr, uint left,
49+
uint right, uint pivot) -> uint {
50+
51+
fn compare[T](lteq[mutable T] compare_func, vec[mutable T]arr,
52+
uint arr_idx, &T arr_value) -> bool {
53+
54+
ret compare_func(arr.(arr_idx),arr_value);
55+
}
56+
57+
auto pivot_value = arr.(pivot);
58+
swap[T](arr, pivot, right);
59+
let uint storage_index = left;
60+
let uint i = left;
61+
while (i<right) {
62+
if (compare[T](compare_func, arr, i, pivot_value)) {
63+
swap[T](arr, i, storage_index);
64+
storage_index += 1u;
65+
}
66+
i += 1u;
67+
}
68+
swap[T](arr, storage_index, right);
69+
ret storage_index;
70+
}
71+
72+
fn qsort[T](lteq[mutable T] compare_func, vec[mutable T] arr, uint left,
73+
uint right) {
74+
75+
if (right > left) {
76+
auto pivot = (left+right)/2u;
77+
auto new_pivot = part[T](compare_func, arr, left, right, pivot);
78+
if (new_pivot == 0u) {
79+
ret;
80+
}
81+
qsort[T](compare_func, arr, left, new_pivot - 1u);
82+
qsort[T](compare_func, arr, new_pivot + 1u, right);
83+
}
84+
}
85+
86+
fn quick_sort[T](lteq[mutable T] compare_func, vec[mutable T] arr) {
87+
88+
if (len[mutable T](arr) == 0u) {
89+
ret;
90+
}
91+
qsort[T](compare_func, arr, 0u, (len[mutable T](arr)) - 1u);
92+
}
93+
4294
// Local Variables:
4395
// mode: rust;
4496
// fill-column: 78;

trunk/src/test/run-pass/lib-qsort.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use std;
2+
3+
fn check_sort(vec[mutable int] v1, vec[mutable int] v2) {
4+
auto len = std._vec.len[int](v1);
5+
6+
fn ltequal(&int a, &int b) -> bool {
7+
ret a <= b;
8+
}
9+
auto f = ltequal;
10+
std.sort.quick_sort[int](f, v1);
11+
auto i = 0u;
12+
while (i < len) {
13+
log v2.(i);
14+
assert (v2.(i) == v1.(i));
15+
i += 1u;
16+
}
17+
}
18+
19+
20+
fn main() {
21+
{
22+
auto v1 = vec(mutable 3,7,4,5,2,9,5,8);
23+
auto v2 = vec(mutable 2,3,4,5,5,7,8,9);
24+
check_sort(v1, v2);
25+
}
26+
27+
{
28+
auto v1 = vec(mutable 1,1,1);
29+
auto v2 = vec(mutable 1,1,1);
30+
check_sort(v1, v2);
31+
}
32+
33+
{
34+
let vec[mutable int] v1 = vec(mutable);
35+
let vec[mutable int] v2 = vec(mutable);
36+
check_sort(v1, v2);
37+
}
38+
39+
{
40+
auto v1 = vec(mutable 9);
41+
auto v2 = vec(mutable 9);
42+
check_sort(v1, v2);
43+
}
44+
45+
{
46+
auto v1 = vec(mutable 9,3,3,3,9);
47+
auto v2 = vec(mutable 3,3,3,9,9);
48+
check_sort(v1, v2);
49+
}
50+
51+
}
52+
53+
// Local Variables:
54+
// mode: rust;
55+
// fill-column: 78;
56+
// indent-tabs-mode: nil
57+
// c-basic-offset: 4
58+
// buffer-file-coding-system: utf-8-unix
59+
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
60+
// End:
61+
62+

0 commit comments

Comments
 (0)