基数排序如何实现?(Pascal语言)

来源:百度知道 编辑:UC知道 时间:2024/06/04 17:18:02
基数排序(多关键字排序)

拜托,基数排序不是多关键字排序。
以下是快排的多关键字排序:
program qsort_hwr;

const
maxn=1000000;

var
a:array[0..maxn,1..2]of longint;
n,i:longint;

procedure qsort(x,y:longint);
var
i,j,m1,m2:longint;
begin
i:=x;
j:=y;
m1:=a[(x+y) shr 1,1];
m2:=a[(x+y) shr 1,2];
repeat
while (a[i,1]<m1)or((a[i,1]=m1)and(a[i,2]<m2)) do inc(i);
while (a[j,1]>m1)or((a[j,1]=m1)and(a[j,2]>m2)) do dec(j);
if i<=j then begin
a[0]:=a[i];
a[i]:=a[j];
a[j]:=a[0];
inc(i);
dec(j);
end;
until i>j;
if x<j then qsort(x,j);
if i<y then qsort(i,y);
end;

begin
while not eof do begin
readln(n);
for i:=1 to n do readln(a[i,1],a[i,2]);
qsort(1,n);
writeln;
for i:=1 to n do writeln(a[i,1],' ',a[i,2]);
end;
end.