-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRandTest.b
71 lines (62 loc) · 1.35 KB
/
RandTest.b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
implement RandTest;
include "sys.m";
include "draw.m";
sys: Sys;
RandTest: module {
init: fn(c: ref Draw->Context, a: list of string);
};
init(c: ref Draw->Context, a: list of string){
sys = load Sys Sys->PATH;
test := array[1000] of int;
for(i := 0; i < len test; i++)
test[i] = i;
randomise(test);
sys->print("before:\n");
printarr(test);
parasort(test, nil);
sys->print("after:\n");
printarr(test);
}
randomise(arr: array of int){
c := chan of int;
d := chan of int;
for(i := 0; i < len arr; i++)
spawn get(arr, i, c, d);
for(i = 0; i < len arr; i++)
<-d;
for(i = 0; i < len arr; i++)
arr[i] = <-c;
}
get(arr: array of int, i: int, c: chan of int, d: chan of int){
t := arr[i];
d<- = 0;
c<- = t;
}
printarr(arr: array of int){
for(x := 0; x < len arr; x++)
sys->print("[%d] = %d\n", x, arr[x]);
}
swap(arr: array of int, m: int, n: int){
t := arr[m];
arr[m] = arr[n];
arr[n] = t;
}
parasort(arr: array of int, notify: chan of int){
if(len arr == 2){
if(arr[0] > arr[1])
swap(arr, 0, 1);
}else if(len arr > 2){
pv := arr[store := 0];
swap(arr, len arr - 1, 0);
for(x := 0; x < len arr - 1; x++)
if(arr[x] < pv)
swap(arr, x, store++);
swap(arr, store, len arr - 1);
c := chan of int;
spawn parasort(arr[0:store], c);
spawn parasort(arr[store+1:], c);
<-c; <-c;
}
if(notify != nil)
notify<- = 0;
}