标签:style io color os sp for strong on bs
C++的快排算法代码
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<time.h>
#define N 5000
using namespace std;
template <class T>
void qsort(T *l,T *r)
{ T *i,*j,*k;
int t;
i=l;j=r-1;k=i+rand()%(r-l);
t=*k;
while(i<j)
{
while(i<k && *i<=t)
i++;
*k=*i;k=i;
while(k<j && *j>=t)j--;
*k=*j;k=j;
}
*k=t;
if(k-l>1)qsort<T>(l,k);
if(r-k>1)qsort<T>(k,r);
}
void main()
{
srand(time(0));
int a[N];
for(int i=0;i<N;++i)
a[i]=rand()%(N*5);
qsort<int>(a,a+N-1);
/*for(i=0;i<N;++i) cout<<setw(5)<<a[i]; cout<<endl; */
for(i=1;i<N;++i)
if(a[i-1]>a[i])
{
cout<<false<<endl;return;
}
cout<<true<<endl;
格式有点乱了,希望对大家有帮助!
标签:style io color os sp for strong on bs
原文地址:http://blog.csdn.net/u014180504/article/details/41660911