- 首页 > it技术 > >
#include
using namespace std;
template< typename T >
void sort( T* a, int n){
if(n<=1) return;
//递归退出条件,否则会出现段错误
if(n==2){
if(a[1]<*a) swap(a[1],*a);
return;
}
swap(*a,a[n>>1]);
T flag = a[0];
//保存分界值
T* left = a+1;
T* right = a+n-1;
while(leftwhile( leftwhile( *right>=flag && right>a ) --right;
if( left}
swap(*a,*right);
sort(a,right-a);
sort(right+1,(n-1)-(right-a));
}
int main()
{
int a[10240];
for(int i=0;
i<10240;
i++)
a[i] = 10240-i;
time_t t = time(NULL);
sort(a,10240);
cout << "time: " << time(NULL)-t << endl;
for(int i=0;
i<10;
i++)
cout << a[i] << ' ';
cout << endl;
}
推荐阅读