Cod sursa(job #288127)

Utilizator punkistBarbulescu Dan punkist Data 25 martie 2009 16:19:59
Problema Sortare prin comparare Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1.14 kb
const
  Max = 500000;

type
  List = array[1..Max] of longint;

var
  Data: List;
  n,i:longint;
  f,f2:text;

{ QUICKSORT sorts elements in the array A with indices between  }
{ LO and HI (both inclusive). Note that the QUICKSORT proce-    }
{ dure provides only an "interface" to the program. The actual  }
{ processing takes place in the SORT procedure, which executes  }
{ itself recursively.                                           }

procedure QuickSort(var A: List; Lo, Hi: longint);

procedure Sort(l, r: longint);
var
  i, j, x, y: longint;
begin
  i := l; j := r; x := a[(l+r) DIV 2];
  repeat
    while a[i] < x do i := i + 1;
    while x < a[j] do j := j - 1;
    if i <= j then
    begin
      y := a[i]; a[i] := a[j]; a[j] := y;
      i := i + 1; j := j - 1;
    end;
  until i > j;
  if l < j then Sort(l, j);
  if i < r then Sort(i, r);
end;

begin {QuickSort};
  Sort(Lo,Hi);
end;

begin
assign(f,'algsort.in');
assign(f2,'algsort.out');
reset(f);
readln(f,n);
for i:=1 to n do read(f,data[i]);
close(f);
Quicksort(Data,1,n);
rewrite(f2);
for i:=1 to n do write(f2,data[i],' ');
close(f2);
end.