<pre name="code" class="cpp"> //快速排序
#include <iostream>
using namespace std;
#include <ctime>//计算排序时间
typedef int T;
void sort (T a[], int n)//(T* a, int n)
{
if(n<=1) return;
swap(*a, a[n>>1]);
T* L=a+1;
T* R=a+n-1;
T v=*a;
while(L<R) {
while(*L<v&&L<R) L++;
while(*R>=v&&R>a) R--;
if(L<R)
swap(*L,*R);
}
if(v>*R) swap(*a,*R);
sort(a,R-a);
sort(R+1,n-(R-a)-1);
}
int main()
{
const int N=10240;
int a[N];
for (int i=0;i<N;i++)
a[i]=N-i;
time_t t1=time(NULL);
sort (a,N);
time_t t2=time(NULL);
cout<<"time:"<<t2-t1<<endl;//排序时间
for (int i=0;i<10;i++ )
cout<<a[i]<<' ';
cout<<endl;
return 0;
}
快速排序
最新推荐文章于 2024-07-17 10:35:12 发布