点击(此处)折叠或打开
- /*
- **选择排序,10000个数据27.32秒
- */
- #include using namespace std;
- void sort(int* a,const int len)
- {
- for(int i=0;i<len;i++)
- {
- int temp = i;
- for(int j=i;j<len;j++)
- {
- if(a[temp] < a[j]) {
- temp = j;
- }
- }
- int t = a[temp];
- a[temp] = a[i];
- a[i] = t;
- }
- }
- /*
- **插入排序,10000个数据21.13秒
- */
- void sort(int* a,const int len)
- {
- int temp,j;
- for(int i=1;i=0&&a[j]<temp;j--)
- {
- a[j+1] = a[j];
- }
- a[j+1] = temp;
- }
- }
- /*
- **快速排序,10000个数据0.23秒!
- */
- void sort(int* a,const int len)
- {
- if(len<=0) return;
- int L = 0;
- int R = len-1;
- int temp = a[L];
- while(L<R) {
- while( L=a[R] ) R--;
- a[L] = a[R];
- while( L<R && temp<=a[L] )L++;
- a[R] = a[L];
- }
- a[L] = temp;
- sort(a,L);
- sort(a+L+1,len-1-L);
- }
- /*
- **测试代码
- */
- #include
- #include
- #include
- #include
- using namespace std;
- int main(int argc,char* argv[])
- {
- srand(time(0));
- int a[NUM] ;
- for(int i=0;i<NUM;i++)
- {
- a[i] = rand()%NUM;
- }
- for(int i=0;i<20;i++)
- {
- cout << a[i] << " ";
- }
- cout<< "..." << endl;
- clock_t beg = clock();
- sort(a,a+NUM);
- clock_t end = clock();
- for(int i=0;i<20;i++)
- {
- cout << a[i] << " ";
- }
- cout<< "..." << endl;
- cout << "共用时 " << (end-beg)*1.0/CLOCKS_PER_SEC << " 秒" << endl;
- return 0;
- }
相关热门文章
给主人留下些什么吧!~~
评论热议