File:  [CSRG BSD Unix] / 43BSDReno / pgrm / pascal / tstpx / src / quicksort.p
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 16:12:57 2018 UTC (8 years, 1 month ago) by root
Branches: MAIN, BSD
CVS tags: HEAD, BSD43reno
BSD 4.3reno

program quicksort(output);
  const n = 100;
  var i,z: integer;
      a: array[1..n] of integer;

  procedure sort(l,r: integer);
    var i,j,x,w: integer;
  begin {quicksort with recursion on both partitions}
    i := l; j := r; x := a[(i+j) div 2];
    repeat
      while a[i] < x do i := i+1;
      while x < a[j] do j := j-1;
      if i <= j then
        begin w := a[i]; a[i] := a[j]; a[j] := w;
          i := i+1; j := j-1
        end
    until i > j;
    if l < j then sort(l,j);
    if l < r then sort(i,r);
  end { sort } ;

begin z := 1729;  {generate random sequence}
  for i := 1 to n do
    a[i] := round(1000 * random(1.0));
  writeln(wallclock);
  sort(1,n);
  writeln(wallclock);
  for i := 1 to n-1 do
   if a[i] > a[i+1] then writeln(i,a[i],a[i+1]);
end .

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.