pascal中的快排

发布网友 发布时间:2022-04-21 23:47

我来回答

4个回答

热心网友 时间:2024-02-16 02:53

{快排(小到大)}

var a:array[1..100]of longint;
i,n:longint;

procere kp(s,t:longint);
var i,j,x,t1:longint;
begin
i:=s;
j:=t;
x:=a[i];
repeat
while (a[j]>=x) and (j>i) do dec(j);
if j>i then begin
t1:=a[i];
a[i]:=a[j];
a[j]:=t1;
end;
while (a[i]<=x) and (i<j) do inc(i);
if i<j then begin
t1:=a[j];
a[j]:=a[i];
a[i]:=t1;
end;
until i=j;
a[i]:=x;
inc(i);
dec(j);
if s<j then kp(s,j);
if i<t then kp(i,t);
end;

begin
readln(n);
for i:=1 to n do read(a[i]);
readln;
kp(1,n);
for i:=1 to n-1 do write(a[i],' ');
writeln(a[n]);
end.

热心网友 时间:2024-02-16 02:54

var
ae:array[1..100000] of longint;
n,i:longint;

function devide (var a:arr;p,r:longint):longint;
var
i,j,x,tmp:longint;
begin
x:=a[r];
i:=p-1;
for j:=p to r-1 do if a[j]<=x then begin
inc (i);
tmp:=a[i];a[i]:=a[j];a[j]:=tmp;
end;
tmp:=a[i+1];a[i+1]:=a[r];a[r]:=a[i+1];
devide:=i+1;
end;

procere quick_sort(var s:arr;l,r:longint);
var
t:longint;
begin
t:=devide (s,l,r);
quick_sort (s,l,t-1);
quick_sort (s,t+1,r);
end;

begin
readln (n);
for i:=1 to n do read (ae[i]);
quick_sort (ae,1,n);
for i:=1 to n do write (ae[i],' ');
readln
end.

热心网友 时间:2024-02-16 02:54

var
i,j,k,n:longint;
a:array[1..50000]of longint;
procere kp(r,l:longint);
var i,j,x,t:longint;
begin
i:=r;j:=l;x:=a[(i+j)div 2];
repeat
while a[i]<x do inc(i);
while a[j]>x 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 i<l then kp(i,l);
if j>r then kp(r,j);
end;
begin
readln(n);
for i:=1 to n do
read(a[i]);
readln;
kp(1,n);
for i:=1 to n do
write(a[i],' ');
end.

热心网友 时间:2024-02-16 02:55

procere qsort(low,high:longint);
var
i,j,m,t:longint;
begin
i:=low;j:=high;m:=a[(i+j) div 2];
repeat
while a[i]<m do i:=i+1;
while a[j]>m do j:=j-1;
if i<=j then begin
t:=a[i];
a[i]:=a[j];
a[j]:=t;
i:=i+1;
j:=j-1;
end;
until i>j;
if low<j then qsort(low,j);
if i<high then qsort(i,high);
end;

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com