博客新人起步,先来一个随机化快排试试手吧。
const
maxn=1000000;
var
n,i:longint;
a:array[1..maxn] of longint;
procedure qsort(l,r:longint);
var
mid,x,i,j:longint;
begin
randomize;
mid:=random(r-l+1)+l;
x:=a[mid];a[mid]:=a[l];
i:=l;j:=r;
while i<j do begin
while (i<j) and (a[j]>=x) do dec(j);if i<j then begin a[i]:=a[j];inc(i);end;
while (i<j) and (a[i]<=x) do inc(i);if i<j then begin a[j]:=a[i];dec(j);end;
end;
a[i]:=x;
if l<i-1 then qsort(l,i-1);
if i+1<r then qsort(i+1,r);
end;
begin
read(n);
for i:=1 to n do read(a[i]);//a[i]:=random(maxlongint);
qsort(1,n);
for i:=1 to n do
if i mod 10=0 then writeln(a[i])
else write(a[i],' ');
end.
2021年更新(sort函数)
#include<iostream>
#include<algorithm>
using namespace std;
int n;
int a[10000];
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n);
for(int i=0;i<n;i++) cout<<a[i]<<' ';
return 0;
}