compiler-design-eth/javali_tests/HWB/Quicksort.javali

61 lines
1.2 KiB
Plaintext

// Overall test of arrays, loops, etc. that does a simple quicksort.
class Record {
int a ;
}
class Main {
Record [] a;
int i;
void swap(Record r1, Record r2) {
int temp;
temp = 0+1;
temp = r1.a;
r1.a = r2.a;
r2.a = temp;
}
void sort(int left, int right) {
int i,j;
int m;
m = (a[left].a + a[right].a) / (1 + 1);
i = left;
j = right;
while (i <= j) {
while (a[i].a < m) { i = i+1; }
while (a[j].a > m) { j = j-1; }
if (i <= j) {
swap(a[i], a[j]);
i = i+1;
j = j-1;
}
}
if (left < j) { sort(left,j); }
if (i < right) { sort(i,right); }
}
void main() {
int SIZE;
int j;
SIZE = 10;
a = new Record[SIZE * 1];
j = 0;
while (j < SIZE) {
a[j] = new Record();
a[j].a = read();
j = j + 1;
}
sort(0, SIZE-1);
j = 0;
while (j < SIZE) {
i = a[j].a;
write(i);
writeln();
j = j + 1;
}
}
}