BSD 2 development
[unix-history] / src / pascal / tests / quicksort.p
CommitLineData
bb56dd76
BJ
1program quicksort(output);
2 const n = 100;
3 var i,z: integer;
4 a: array[1..n] of integer;
5
6 procedure sort(l,r: integer);
7 var i,j,x,w: integer;
8 begin {quicksort with recursion on both partitions}
9 i := l; j := r; x := a[(i+j) div 2];
10 repeat
11 while a[i] < x do i := i+1;
12 while x < a[j] do j := j-1;
13 if i <= j then
14 begin w := a[i]; a[i] := a[j]; a[j] := w;
15 i := i+1; j := j-1
16 end
17 until i > j;
18 if l < j then sort(l,j);
19 if l < r then sort(i,r);
20 end { sort } ;
21
22begin z := 1729; {generate random sequence}
23 for i := 1 to n do
24 begin z := (131071*z) mod 2147483647; a[i] := z
25 end ;
26 writeln(wallclock);
27 sort(1,n);
28 writeln(wallclock);
29 for i := 1 to n-1 do
30 if a[i] > a[i+1] then writeln(i,a[i],a[i+1]);
31end .