我最喜欢的快速排序算法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
program qsort;
var n,p:integer;
a:array[0..100000] of integer;
procedure qs(l,r:integer);//假设被排序的数组是a,且快排后按升序排列)
var i,j,m,t:integer;
begin
i:=l;
j:=r;//(l(left),r(right)表示快排的左右区间)
m:=a[(l+r)div2];//注意:本句不能写成:m:=(l+r)div2;
repeat
while a[i]<m do inc(i);
while a[j]>m do dec(j);//若是降序把'<'与‘>'互换;
if i<=j then
begin
t:=a[i];
a[i]:=a[j];
a[j]:=t;
inc(i);
dec(j);
end;
until i>j;
if l<j then qs(l,j);//递归查找左区间
if i<r then qs(i,r);//递归查找右区间
end;

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×