Virtual Object
::
Programs
:: Поразрядная сортировка |
||||
Наша кнопка |
||||
Поразрядная сортировка {$APPTYPE CONSOLE}
uses SysUtils;
var a,dest:array [0..10000] of longint;
count:array [0..10] of longint;
k,n,i:longint;
⁄⁄-------------------------------------------
function get_digit(x,y:longint):longint;
var temp,i:longint;
begin
temp:=0;
for i:=1 to y do
begin
temp:=x mod 10;
x:=x div 10;
end;
get_digit:=temp;
end;
⁄⁄-------------------------------------------
procedure radixsort(first:boolean; x:longint);
var i,j,t,temp:longint;
begin
fillchar(count,sizeof(count),0);
for i:=1 to n do
if first then inc(count[get_digit(a[i],x)])
else inc(count[get_digit(dest[i],x)]);
t:=1;
for j:=0 to 9 do
begin
temp:=count[j];
count[j]:=t;
t:=t+temp;
end;
for i:=1 to n do
if first then
begin
dest[count[get_digit(a[i],x)]]:=a[i];
inc(count[get_digit(a[i],x)]);
end else
begin
a[count[get_digit(dest[i],x)]]:=dest[i];
inc(count[get_digit(dest[i],x)]);
end;
if x<k then radixsort(not first,x+1);
end;
⁄⁄-------------------------------------------
begin
assign(input,′input.txt′); reset(input);
assign(output,′output.txt′); rewrite(output);
readln(n);
k:=0;
for i:=1 to n do
begin
readln(a[i]);
if k<length(IntToStr(a[i])) then k:=length(IntToStr(a[i]));
end;
radixsort(true,1);
for i:=1 to n do
if odd(k) then writeln(dest[i]) else writeln(a[i]);
end.
|
||||
Object © 2004 - 2005. All rights reserved. |
||||