Các thuật toán sắp xếp
1, Sắp xếp nổi bọt
For i:=1 to N-1 do
For j:=i+1 to N do
If a[i]>a[j] then
Begin
Tam:=a[i];a[i]:=a[j];a[j]:=tam;
End;
Độ phức tạp thuật toán 0(N^2)
Độ phức tạp không gian bộ nhớ 0(N)
2, Sắp xếp nhanh-Quicksort
Procedure QS(L,R:longint);
Var
Begin
If L>=R then exit;i:=L;j:=R;
Giua:=a[(L+R) div 2];
Repeat
While a[i]<giua do inc(i);
While a[j]>giua do dec(j);
If i<=j then
Begin
Tam:=a[i];a[i]:=a[j];a[j]:=tam;
I:=i+1;j:=j-1;
End;
Until i>j;
If i<R then QS(i,R);
If j>L then QS(j,L);
End;
Độ phức tạp thuật toán là 0(N.logN)~10^5
Độ phức tạp không gian bộ nhớ 0(N)

Preview text:

Các thuật toán sắp xếp 1, Sắp xếp nổi bọt For i:=1 to N-1 do For j:=i+1 to N do If a[i]>a[j] then Begin
Tam:=a[i];a[i]:=a[j];a[j]:=tam; End;
Độ phức tạp thuật toán 0(N^2)
Độ phức tạp không gian bộ nhớ 0(N)
2, Sắp xếp nhanh-Quicksort Procedure QS(L,R:longint); Var Begin
If L>=R then exit;i:=L;j:=R; Giua:=a[(L+R) div 2]; Repeat
While a[i]While a[j]>giua do dec(j); If i<=j then Begin
Tam:=a[i];a[i]:=a[j];a[j]:=tam; I:=i+1;j:=j-1; End; Until i>j; If iIf j>L then QS(j,L); End;
Độ phức tạp thuật toán là 0(N.logN)~10^5
Độ phức tạp không gian bộ nhớ 0(N)