刚学cstdlib的qsort没几天,又学了STL的sort
显然头文件是algorithm
为了测效率,在linux翻了倒去,最后连arbiter都用上。
发现比qsort form cstdlib和手写的qsort 略慢一点,比heap和归并略快一点。
基本上是可以用的。(毕竟我还常用heap和归并替代qsort)
比起cstdlib来说最好的就是灵活
bool cmp(T a,T b){...}
怎么比较随你,只要返回true或false即可
为false就交换,为true就不交换。
sort(*a,*b,cmp);
a,b是头指针和尾指针,由于数组等价于指针,所以代码各外简单。
注意原型是std::sort所以要using namespace std;(写原型亦可,随你)

裸排序(升序)
#include <cstdio>
#include <cstring>
#include <algorithm>
// using namespace std;

int cmp(int a,int b)
{
return a<b;
}
int a[500001];

int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
int n;
scanf("%d",&n);
for(int i=0;i<=n-1;i++)
scanf("%d",&a[i]);
std::sort(a,a+n,cmp);
for(int i=0;i<=n-1;i++)
printf("%d ",a[i]);
}
