Les Algorithmes de tri

dali101

عضو نشيط
إنضم
5 نوفمبر 2006
المشاركات
249
مستوى التفاعل
39
Tri par selection

program tri;
uses wincrt;
type tab=array[1..100]of integer;
var t:tab;
n:integer;

function posmin (a:tab; k:integer):integer;
var i,fonc: integer;
begin
pmin:=k;
for i:=k+1to n do
if a<a[pmin] then
pmin:=i;
posmin:=pmin;
end;

procedure permuter(var x,y:integer);
var aux:integer;
begin
aux:=x;
x:=y;
y:=aux;
end;

procedure triselection (var a:tab;x:integer);
var p,i:integer;
begin
for i:=1 to x-1 do
begin
p:=posmin(a,i);
if a[p] <> a then
permuter(a,a[p]);
end;
end;

procedure saisie(var b:tab;var x:integer);
var i:integer;
begin
writeln('donner la taille');readln(x);
for i:=1 to x do
readln(b);
end;

procedure affiche(s:tab;x:integer);
var j:integer;
begin
for j:=1 to x do
writeln(s[j]);
end;

{Programme principal}
begin
saisie(t,n);
triselection(t,n);
affiche(t,n);
end.


Tri a bulles

program tri;
uses wincrt;
type tab=array[1..100]of integer;
var t:tab;
n:integer;

procedure permuter(var x,y:integer);
var aux:integer;
begin
aux:=x;
x:=y;
y:=aux;
end;

procedure triabulles (var b:tab;x:integer);
var p,i:integer; permut:boolean;
begin
repeat
permut:=false;
for i:=1 to x-1 do
begin
if b>b[i+1] then
begin
permut:=true ;
permuter(b,b[i+1]);
end;
end;
until permut= false;
end;

procedure saisie(var b:tab;var x:integer);
var i:integer;
begin
writeln('donner la taille');readln(x);
for i:=1 to x do
readln(b);
end;

procedure affiche(s:tab;x:integer);
var j:integer;
begin
for j:=1 to x do
writeln(s[j]);
end;
{Programme principal}
begin
saisie(t,n);
triabulles(t,n);
affiche(t,n);
end.


Tri par Insertion
program tri;
uses wincrt;
type tab=array[1..100]of integer;
var t:tab;
n:integer;

procedure triinsertion (var b:tab;x:integer);
var aux,j,i,p:integer;

Procedure decaler (var a:tab;deb:integer;var fin:integer);
begin
fin:=deb;
while (fin>0) and (a[fin]> aux) do
begin
a[fin+1]:=a[fin];
fin:=fin-1;
end;
end;

begin
for i:=2 to x do
begin
aux:=b;
decaler(b,i-1,p);
b[p]:=aux;
end;
end;

procedure saisie(var b:tab;var x:integer);
var i:integer;
begin
writeln('donner la taille');readln(x);
for i:=1 to x do
readln(b);
end;

procedure affiche(s:tab;x:integer);
var j:integer;
begin
for j:=1 to x do
writeln(s[j]);
end;

{Programme principal}
begin
saisie(t,n);
triinsertion(t,n);
affiche(t,n);
end.

:kiss:
 

Ahmed.tn11

عضو
إنضم
2 جانفي 2008
المشاركات
1.705
مستوى التفاعل
1.715
mon ami faut pas poster les sujets comme ça vous devez le mettre dans un fichier texte
 
أعلى