quicksort
program quicksort; const n=31; var a: array [1..n] of integer; procedure aswap (i, j: integer); var t : integer; begin t := a[i]; a[i] := a[j]; a[j] := t; end; procedure swap (var a, b: integer); var t : integer; begin t := a; a := b; b := t; end; procedure quick (r, s: integer); var i, k, x: integer; begin i := r; k := s; x := a[(i+k) div 2]; repeat while a[i] < x do i:= i+1; while a[k] > x do k:=k-1; if i<=k then begin aswap (i,k); i:=i+1; k:=k-1; end; until i>k; if r<k then quick(r,k); if i<s then quick(i,s); end; procedure fill; var i: integer; begin randomize; for i:= 1 to n do a[i]:= random(100); end; procedure print; var i: integer; begin for i:= 1 to n do write(a[i],' '); writeln; end; procedure check; var i:integer; ok: boolean; begin ok:=true; for i:=1 to n-1 do if a[i] > a[i+1]then ok:=false; if not ok then writeln('Chyba'); end; begin fill; print; quick (1, n); print; check; readln; end.
quicksort.txt · Last modified: 2014/04/24 12:24 by 127.0.0.1