Guest User

Untitled

a guest
Feb 18th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 0.66 KB | None | 0 0
  1. program Qucksort;
  2. {$R+}
  3. {$O-}
  4. {$APPTYPE CONSOLE}
  5. uses
  6.   SysUtils;
  7. var n,i:longint;
  8.     a: packed array[1..100000] of integer;
  9. procedure Qsort(l,r:longint);
  10. var i,j,x,y:longint;
  11. begin
  12.   i:=l;
  13.   j:=r;
  14.   x:=a[l+random(r-l)];
  15.   repeat
  16.     while a[i]<x do
  17.       inc(i);
  18.     while a[j]>x do
  19.       dec(j);
  20.     if i<=j then
  21.       begin
  22.         y:=a[i];
  23.         a[i]:=a[j];
  24.         a[j]:=y;
  25.         inc(i);
  26.         dec(j);
  27.       end;
  28.   until (i>j) ;
  29.   if l<j then
  30.     Qsort(l,j);
  31.   if i<r then
  32.     Qsort(i,r);
  33. end;
  34. begin
  35.   reset(input,'input.txt');
  36.   rewrite(output,'output.txt');
  37.   read(n);
  38.   for i := 1 to n do
  39.     read(a[i]);
  40.   Qsort(1,n);
  41.   for i := 1 to n do
  42.     write(a[i],' ');
  43. end.
Add Comment
Please, Sign In to add comment