destr0y

Lab2_2

Mar 21st, 2020
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var a: array of integer := (20, 18, 7, 4, 5, 14, 6, 1, 12, 9);
  2.  
  3. procedure Partition (L,R: integer);
  4. var
  5.   i, j: integer;
  6.   w, x: integer;
  7. begin
  8.   if L = R then Exit;
  9.   i := L; j := R;
  10.   x := a[(L + R) div 2 - 1];
  11.   repeat
  12.     while a[i - 1] < x do i := i + 1;
  13.     while a[j - 1] > x do j := j - 1;
  14.     if i <= j then
  15.     begin
  16.       w := a[i - 1]; a[i - 1] := a[j - 1]; a[j - 1] := w;
  17.       i := i + 1; j := j - 1;
  18.     end;
  19.   until i > j;
  20.   if L < j then Partition(L, j);
  21.   if i < R then Partition(i, R);
  22. end;
  23.  
  24.  
  25. begin
  26.   writeln(a);
  27.   Partition(1, a.Length);
  28.   writeln(a);
  29. end.
Add Comment
Please, Sign In to add comment