Advertisement
huyhung94

Giải thuật Quick Sort

Oct 6th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 0.63 KB | None | 0 0
  1. program QuickSort;
  2. Procedure Partition_Sort(a,t,p,j);
  3.     begin
  4.         i:=t+1;
  5.         j:=p; chot:=a[t];
  6.         Repeat
  7.             while( (a[i]<chot) and (i<=j) ) do
  8.                 i:=i+1;
  9.             while((a[i]>chot) and (i<j)) do
  10.                 j:=j-1;
  11.             if (i<j) then
  12.                 begin
  13.                     temp:=a[i]; a[i]:=a[j]; a[j]:=temp;
  14.                     i:=i+1;
  15.                     j:=j-1;
  16.                 end;
  17.         until (i>=j);
  18.         temp:=a[t]; a[t]:=a[j]; a[j]:=temp;
  19.     End;
  20.  
  21. Procedure Quick_Sort(a,t,p);
  22.     begin
  23.         if t<p then
  24.             begin
  25.                 Partition_Sort(a,t,p,j);
  26.                 Quick_sort(a,t,j-1);
  27.                 Quick_sort(a,j+1,p);
  28.             end;
  29.     End;
  30. BEGIN
  31.     Nhập các phần tử ai;
  32.     Quick_Sort(a,1,n);
  33.     In dãy đã sắp xếp;
  34.     readln;
  35. END.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement