[DS&A]基本排序比较

来源:互联网 发布:种草网络语的英文 编辑:程序博客网 时间:2024/05/22 09:01

问题:使用clock函数显示为零

#include<iostream>#include<algorithm>#include<ctime>using namespace std;//冒泡void bubblesort(int a[],int cnt){  bool flag = true;  while(flag)  {flag = false;for(int i = 1; i < cnt ;i++){  if(a[i-1] > a[i])  {swap(a[i-1],a[i]);flag = true;  }}--cnt;  }}//插入法,因交换次数少,比冒泡略好void insertsort(int a[],int n){  int j,p;  for(int i =1; i < n; i++)  {if(a[i] >a[i-1]) continue;p = a[i];int j;for(j = i-1; j>=0 && p < a[j]; j--)  a[j+1] = a[j];a[j+1] = p;//注意j的变化  }}//选择法int findmin(int a[],int left,int right){  int min=left;  for(int i= left+1; i <= right; i++)  {if(a[i] < a[min]) min = i;  }  return min;}void choosesort(int a[],int n){  int min; for(int i = 0; i < n-1; i++) {   min = findmin(a,i,n-1);   swap(a[min],a[i]); }}//快速排序void quicksort(int* a, int n){if(n<=1) return;if(n==2){if(a[1]<a[0]) swap(a[1],a[0]);return;}swap(a[n/2],a[0]);  //把分界值交换到最左边int jie=a[0];int* L=a+1;int* R=a+n-1;while(L<R){while(L<R&&*L<jie) ++L;while(a<R&&!(*R<jie)) --R;if(L<R) swap(*L,*R);}if(*R<jie) swap(*R,a[0]);quicksort(a, R-a);quicksort(R+1,n-1-(R-a));}int main(){  int a[10] = {18,25,63,45,9,125,378,56,78,95}; //   clock_t t1 = clock();//   bubblesort(a,10);//   clock_t t2 = clock();//   cout << "冒泡排序:" <<double(t2-t1)/CLOCKS_PER_SEC << endl;//     clock_t t1 = clock();  insertsort(a,10);  clock_t t2 = clock();  cout << "插入排序:" <<double(t2-t1)/CLOCKS_PER_SEC << endl;//   choosesort(a,5);//   quicksort(a,5);  for(int i = 0;i < 10; i++)  {cout << a[i] << " ";  }  cout << endl;  return 0;}


0 0
原创粉丝点击