Sap xep

1. Sap xep theo kieu lua chon:

Procedure sapxepluachn (K[1..n]);

For i:=1 to n do

Begin

m := i;

For j := i+1 to n do

If K[j] < K[m] then m:=j

If m # i then

Begin

tam := K[i];

K[ i] := K[m];

K[m] := tam;

End;

End;

Return;

----------------------------------------------------------------

2.Sap xem them dan:

Procedure sapxepthemdan (K[1..n]);

For i:=2 to n do

Begin

m := i; x := K[i]

While (m>1) and (K[m-1] < K[m]) do

Begin

K[m] := K[m-1]; m := m -1;

End;

If m # i then K[m] := x;

End;

Return;

------------------------------------------------------------------

3.Sap xep kieu doi cho:

Procedure sapxepdoicho (K[1..n]);

For i := 1 to n-1 do

For m := n downto i+1 do

If K[m-1] > K[m] then

K[m-1] <-> K[m]

Return;

-------------------------------------------------------------------

4.Sap xep hoa nhap:

Procedure sapxephoanhap (K,n);

d:=1;

Whild d<n do

Begin

b:=1; l:=b+d-1; m:=b+2d-1;

While m<=n do

Begin

tron(k,b,l,m);

b:=m+1; l:=b+d-1; m:=b+2d-1;

End;

If l<n then tron(k,b,l,m);

d:=2d;

End;

Return;

------------------------------------------------------------------

5.QuickSort........................

Procedure Quick-sort(k,lb,ub);

If lb < ub then

Begin

chot:=k[lb]; i:=lb+1; j:=ub;

While i<j Do

Begin

While (i<= ub) And (k[i]<chot) Do

i :=i+1;

While (i>=lb) Dnd (k[j]>chot) Do

j:=j-1;

If i<j Then k[j]<-> k[i]

End;

If j<lb Then Quick-sort(k,lb+1,ub)

Else If i >ub Then

Begin

k[lb] <-> k[ub];

Quick-sort(k,lb,ub-1)

End;

Else

Begin

k[lb]<->k[j]

Quick-sort(k,lb,j-1);

Quick-sort(k,j+1,ub);

End;

End;

Return;

Bạn đang đọc truyện trên: AzTruyen.Top

Tags: