{************************************************
                 Hugo Eti‚vant
    http://www.multimania.com/cyberzoide/
      email : cyberzoide@multimania.com
     (pour une aide en Turbo Pascal 7.0)
*************************************************}
(************************************************
     VISUALISATION GRAPHIQUE
     D'UN ALGORITHME DE TRI NON RAPIDE

     Le nuage de point forme un cercle dont
     la concavit‚ tend progressivement vers
     l'infini afin de donner une bissectrice.

     Effet graphique ‚tonnant !
************************************************)

program algotri2;

uses graph;

const nmax=480;

type tab=array[1..nmax] of integer;

procedure init(var t:tab; test:boolean; n1,n2,a,b:integer);
var i:integer;
    test1,test2:boolean;
begin
test1:=(n2-n1)>=0;
test2:=(b-a)>=0;
 if (test1 and test2) then
  begin
  if test then for i:=n1 to n2 do t[i]:=0 {initialisation … z‚ro}
          else
              begin   {initialisation al‚atoire}
              randomize;
              for i:=n1 to n2 do t[i]:=random(b-a+1)+a;
              end;
  end;
if not(test2) then writeln('Erreur dans <Init> : l''intervalle al‚atoire est invalide.');
if not(test1) then writeln('Erreur dans <Init> : les bornes du tableau sont invalides.');
end;

{procedure pause;
begin
writeln('Appuyez sur une touche pour continuer...');
repeat until keypressed;
clrscr;
end; }

procedure affichage(t:tab; n1,n2:integer);
var i,j:integer;
begin
if (n2-n1)>=0 then
begin
j:=0;
for i:=n1 to n2 do
    begin
    writeln('Case nø',i,'=',t[i]);
    inc(j);
    {if (j mod 20) =0 then pause; }
    end;
end
else writeln('Erreur dans <affichage> : le tableau ne contient aucun ‚l‚ment.');
end;

procedure tri(var t:tab);
var temp:integer;
    i,j,k:longint;
begin
for j:=1 to nmax do
    begin
    for i:=1 to nmax-1 do
        begin
        if t[i]>t[i+1] then
           begin
           temp:=t[i+1];   {permutation}
           t[i+1]:=t[i];
           t[i]:=temp;
           end;
        end;
    cleardevice;
    moveto(t[1],1);
    for k:= 1 to nmax do {lineto(t[k],k); pour des lignes}
                         line(t[k],k,t[k],k);{ pour des points}
    {pour un delay -> delay(1); }
    end;
end;

var t:tab;
    vga,vgahi,i,j:integer;

BEGIN
Init(t,false,1,nmax,1,nmax); {initialisation du tableau}
initgraph(vga,vgahi,'c:\bp\bgi'); {initialisation graphique}
tri(t); {proc‚dure de tri visualisable}
readln;
closegraph;
END.