procedure SortByCount(a: Vector; kol: integer; var b: Vector); {Сортировка подсчетом} var i, j: integer; c: array[1..50] of integer;
begin for i := 1 to 50 do C[i] := 0; for j := 1 to kol do c[A[j]] := c[A[j]] + 1; for i := 2 to 50 do c[i] := c[i] + c[i - 1]; for j := kol downto 1 do begin b[c[a[j]]] := a[j]; c[a[j]] := c[a[j]] - 1 end end;
begin writeln('Исходный массив'); for i := 1 to n do write(a[i]:4); writeln; SortByCount(a, n, b); writeln('Отсортированный массив'); for i := 1 to n do write(b[i]:4); end.
n = 7;
type
Vector = array[1..n] of integer;
procedure SortByCount(a: Vector; kol: integer; var b: Vector);
{Сортировка подсчетом}
var
i, j: integer;
c: array[1..50] of integer;
begin
for i := 1 to 50 do C[i] := 0;
for j := 1 to kol do
c[A[j]] := c[A[j]] + 1;
for i := 2 to 50 do
c[i] := c[i] + c[i - 1];
for j := kol downto 1 do
begin
b[c[a[j]]] := a[j];
c[a[j]] := c[a[j]] - 1
end
end;
var
a: Vector := (25, 1, 13, 42, 8, 6, 14);
b: Vector;
i: integer;
begin
writeln('Исходный массив');
for i := 1 to n do write(a[i]:4);
writeln;
SortByCount(a, n, b);
writeln('Отсортированный массив');
for i := 1 to n do write(b[i]:4);
end.
Тестовое решение:
Исходный массив
25 1 13 42 8 6 14
Отсортированный массив
1 6 8 13 14 25 42