// Unidade de Ordenação // Desenvolvida por FABIO ANDRIOLI SILVA // Email: fabio.andrioli@terra.com.br // Reune os principais metonos de ordenação // A ordenação e feita por ordem de caracteres, assim serve tanto para ordenaão numerica como de strings
//====================== INSERÇÃO DIRETA ================== procedure TMetodoOrd.InsercaoDireta (var VetorCar: TVetorCar ; nPt: integer ; Ordem: TOrdem); var
i,j,k : integer;
ch: string; {ch: chave a ser inserida} begin
MainForm.nComp :=0;
MainForm.nTroc :=0; for i:= 2 to nPt dobegin{i: posição do elemento a ser inserido}
k:= 1;
j:= i-1;
ch:= VetorCar[i]; While (j >= 1) and (k = 1) dobegin{k, j: início do primeiro segmento}
case Ordem of
Crescente: begin
inc(MainForm.nComp); If (ch < VetorCar[j]) thenbegin
inc(MainForm.nTroc);
VetorCar[j+1] := VetorCar[j];
j:= j-1;
MainForm.AtualizaTab(0,j+1); endelse
k:= j+1 end;
Decrescente: begin
inc(MainForm.nComp); If (ch > VetorCar[j]) thenbegin
inc(MainForm.nTroc);
VetorCar[j+1] := VetorCar[j];
j:= j-1;
MainForm.AtualizaTab(0,j+1); endelse
k:= j+1 end;
//====================== SHELL SORT ================== procedure TMetodoOrd.ShellSort (var VetorCar: TVetorCar ; nPt: integer ; Ordem: TOrdem); var
gap, i, j, k : integer; begin
MainForm.nComp :=0;
MainForm.nTroc :=0;
gap := nPt div 2; while (gap > 0) dobegin for i := (gap + 1) to nPt dobegin
j:= i - gap; while (j > 0) dobegin
k:= j+ gap;
inc(MainForm.nComp);
case Ordem of
Crescente: begin//Ordem Crescente if (VetorCar[j] > VetorCar[k]) thenbegin
Troca(VetorCar[j],VetorCar[k],j,k); endelse
j:=0; end;
Decrescente: begin//Ordem Decrescente if (VetorCar[j] < VetorCar[k]) thenbegin
Troca(VetorCar[j],VetorCar[k],j,k); endelse
j:=0; end; end;
j:= j - gap; end; end;
gap := gap div 2; end; end;
//====================== HEAP SORT ================== procedure TMetodoOrd.HeapSort (var VetorCar: TVetorCar ; nPt: integer ; Ordem: TOrdem); var
i, left, right : integer;
//------------------------------------------------------------------------------ procedure Heapfy(L, R : LongInt); var
i, j : LongInt;
x : String; begin
i := L;
j := 2 * L;
x := VetorCar[i];
case Ordem of
Crescente: begin//Ordem Crescente
if (j < R) and (VetorCar[j + 1] > VetorCar[j]) then
Inc(j);
while (j <= R) and (VetorCar[j] > x) dobegin
VetorCar[i] := VetorCar[j];
MainForm.AtualizaTab(i,j);
inc(MainForm.nTroc);
i := j;
j := j * 2; if (j < R) and (VetorCar[j + 1] > VetorCar[j]) then
Inc(j); end; end;
Decrescente: begin//Ordem Decrescente
if (j < R) and (VetorCar[j + 1] < VetorCar[j]) then
Inc(j);
while (j <= R) and (VetorCar[j] < x) dobegin
VetorCar[i] := VetorCar[j];
MainForm.AtualizaTab(i,j);
inc(MainForm.nTroc);
i := j;
j := j * 2;
inc(MainForm.nComp,2); //soma a comparação do "while" + "if" if (j < R) and (VetorCar[j + 1] < VetorCar[j]) then
Inc(j); end;
end; end;
inc(MainForm.nComp); //soma ultima comparação do "while"
VetorCar[i] := x; end; //------------------------------------------------------------------------------ begin
MainForm.nComp :=0;
MainForm.nTroc :=0;
left := (nPt div 2) + 1;
right := nPt; while (left > 1) dobegin
dec(left);
Heapfy(left,right); end;
for i := right downto 1 dobegin
Troca(VetorCar[1],VetorCar[i],1,i);
Heapfy(1,i - 1); end; end;
for i:=1 to nPt dobegin
min:=i; for j:=i+1 to nPt dobegin
inc(MainForm.nComp); case Ordem of
Crescente: if VetorCar[j]<VetorCar[min] then
Troca(VetorCar[j],VetorCar[min],j,min);
Decrescente: if VetorCar[j]>VetorCar[min] then
Troca(VetorCar[j],VetorCar[min],j,min) end; end; end; end;
//====================== BUBLE SORT ================== procedure TMetodoOrd.BubbleSort (var VetorCar: TVetorCar ; nPt: integer ; Ordem: TOrdem); var
i, j : integer;
begin
MainForm.nComp :=0;
MainForm.nTroc :=0;
for i := 1 to nPt dobegin for j := i+1 to nPt dobegin
inc(MainForm.nComp); case Ordem of
Crescente: if VetorCar[i] > VetorCar[j] then
Troca(VetorCar[i],VetorCar[j],i,j);
Decrescente: if VetorCar[i] < VetorCar[j] then
Troca(VetorCar[i],VetorCar[j],i,j); end; end; end; end;
//====================== SHAKE SORT ================== procedure TMetodoOrd.ShakeSort (var VetorCar: TVetorCar ; nPt: integer ; Ordem: TOrdem); var
i : integer;
Mudou : boolean; begin
MainForm.nComp :=0;
MainForm.nTroc :=0;
repeat
Mudou:= false;
for i:= 1 to nPt-1 dobegin
inc(MainForm.nComp);
case Ordem of
Crescente: if (VetorCar[i] > VetorCar[i+1]) thenbegin
Troca(VetorCar[i],VetorCar[i+1],i,i+1);
Mudou:= true; end;