就相当于把人名按照字典顺序来排序
如:
输入
bax
abc
krd
输出
abc
bax
krd
就相当于把人名按照字典顺序来排序
如:
输入
bax
abc
krd
输出
abc
bax
krd
快速排序:
var
a:array[1..10000] of ansistring;
n:longint;
procedure init;
var
i:longint;
begin
readln(n);
for i:=1 to n do readln(a[i]);
end;
procedure swap(var s1,s2:ansistring);
var
s3:ansistring;
begin
s3:=s1;
s1:=s2;
s2:=s3;
end;
procedure sort(r,l:longint);
var
i,j:longint;
c:ansistring;
begin
i:=r;
j:=l;
c:=a[random(j-i+1)+i];
repeat
while a[i]<c do inc(i);
while a[j]>c do dec(j);
if not(i>j) then
begin
swap(a[i],a[j]);
inc(i); dec(j);
end;
until i>j;
if i<l then sort(i,l);
if j>r then sort(r,j);
end;
procedure ou;
var
i:longint;
begin
for i:=1 to n do writeln(a[i]);
end;
begin
init;
randomize;
sort(1,n);
ou;
end.
冒泡排序:
var
a:array[1..10000] of ansistring;
n,i,j:longint;
c:ansistring;
begin
readln(n);
for i:=1 to n do readln(a[i]);
for i:=1 to n do
for j:=i to n do
if a[i]>a[j] then
begin
c:=a[i];
a[i]:=a[j];
a[j]:=c;
end;
for i:=1 to n do writeln(a[i]);
end.
其实没有那么麻烦的,你可以试试,在pascal中,是可以让字符串直接比较大小的,
就像 if s1 > s2 then writeln(s1);
这是合法的,至于是按 字典顺序比较 还是 按字典顺序我没试过,反正没有影响,你试试吧
var
i,j,k,n:longint;
a:array[1..1000]of string;
procedure qsort(s,t:longint);
var i,j:longint; q,m:string;
begin
i:=l; j:=r; m:=a[random(t-s+1)+s];
repeat
while a[i]<m do inc(i);
while a[j]>m do dec(j);
if i<=j then begin
q:=a[i]; a[i]:=a[j]; a[j]:=q; inc(i); dec(j); end;
until i>j;
if s<j then qsort(s,j);
if i<t then qsort(i,t);
end;
begin
randomize;
readln(n);
for i:=1 to n do readln(a[i]);
qsort(1,n);
for i:=1 to n do writeln(a[i]);
end.
随机化快排版