各种排序的运行时间对比

本文通过C++实现,对比了冒泡排序、插入排序、选择排序、堆排序、非递归归并排序和递归归并排序的时间性能。实验结果显示,堆排序和归并排序在时间上具有优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

冒泡排序


[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //time:34.220s  
  2. #include<cstring>  
  3. #include<iostream>  
  4. #include<fstream>  
  5. #include<algorithm>  
  6. using namespace std;  
  7. class MY{  
  8.     int a;  
  9. //public:  
  10.     friend ostream& operator <<(ostream& os,const MY &my){  
  11.         os<<my.a;return os;  
  12.     }  
  13.     friend istream& operator >>(istream& is,MY& my){  
  14.         is>>my.a;return is;  
  15.     }  
  16. public:  
  17.     bool operator<(const MY& another){  
  18.         return this->a<another.a;  
  19.     }  
  20. };  
  21. template<class T>  
  22. void bubble(T A[],int n)  
  23. {  
  24.     for(int i=n-1;i>=1;i--)  
  25.         for(int j=0;j<i;j++)  
  26.             if(A[j+1]<A[j]) swap(A[j+1],A[j]);  
  27. }  
  28. template<typename T>  
  29. bool check_if_sorted(T A[],int n)  
  30. {  
  31.     for(int i=1;i<n;i++)  
  32.         if(A[i]<A[i-1])return false;  
  33.     return true;  
  34. }  
  35. int main()  
  36. {  
  37.     //测试数据:  
  38.     //输入格式:第一行是n,元素个数。第二行是n个元素。  
  39.     //0.txt 无元素  
  40.     //1.txt 1个元素  
  41.     //2.txt 2个元素  
  42.     //3.txt 5个元素  
  43.     //4.txt 均为相同元素  
  44.     //5.txt 大量相同元素  
  45.     //6.txt 随机元素  
  46.     //7.txt 逆序元素  
  47.     //8.txt 升序元素  
  48.     //有a后缀即为排序后的输出  
  49.     char index[10]="0";  
  50.     for(int i=0;i<9;i++){  
  51.         char file[10];strcpy(file,index);  
  52.         strcat(file,".txt");  
  53.         ifstream fin(file);  
  54.         if(!fin){cerr<<"can't open input file \""<<file<<"\""<<endl;exit(EXIT_FAILURE);}  
  55.         int n;fin>>n;  
  56.         MY* A=new MY [n];  
  57.         for(int i=0;i<n;i++) fin>>A[i];  
  58.         bubble(A,n);  
  59.         cout<<check_if_sorted(A,n)<<endl;  
  60.         char filea[10];strcpy(filea,index);  
  61.         strcat(filea,"a.txt");  
  62.         ofstream fout(filea);  
  63.         for(int i=0;i<n;i++) fout<<A[i]<<" ";  
  64.         index[0]++;  
  65.         delete[] A;  
  66.     }  
  67.     return 0;  
  68. }  

改进的冒泡排序

//time:26.760s
#include<cstring>
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
class MY{
    int a;
//public:
    friend ostream& operator <<(ostream& os,const MY &my){
        os<<my.a;return os;
    }
    friend istream& operator >>(istream& is,MY& my){
        is>>my.a;return is;
    }
public:
    bool operator<(const MY& another){
        return this->a<another.a;
    }
};
template<class T>
void bubble(T A[],int n)
{
    int isSorted=0;
    for(int i=n-1;i>=1&&!isSorted;i--){
        isSorted=1;
        for(int j=0;j<i;j++)
            if(A[j+1]<A[j]) {swap(A[j+1],A[j]);isSorted=0;}
    }
}
template<typename T>
bool check_if_sorted(T A[],int n)
{
    for(int i=1;i<n;i++)
        if(A[i]<A[i-1])return false;
    return true;
}
int main()
{
    //测试数据:
    //输入格式:第一行是n,元素个数。第二行是n个元素。
    //0.txt 无元素
    //1.txt 1个元素
    //2.txt 2个元素
    //3.txt 5个元素
    //4.txt 均为相同元素
    //5.txt 大量相同元素
    //6.txt 随机元素
    //7.txt 逆序元素
    //8.txt 升序元素
    //有a后缀即为排序后的输出
    char index[10]="0";
    for(int i=0;i<9;i++){
        char file[10];strcpy(file,index);
        strcat(file,".txt");
        ifstream fin(file);
        if(!fin){cerr<<"can't open input file \""<<file<<"\""<<endl;exit(EXIT_FAILURE);}
        int n;fin>>n;
        MY* A=new MY [n];
        for(int i=0;i<n;i++) fin>>A[i];
        bubble(A,n);
        cout<<check_if_sorted(A,n)<<endl;
        char filea[10];strcpy(filea,index);
        strcat(filea,"a.txt");
        ofstream fout(filea);
        for(int i=0;i<n;i++) fout<<A[i]<<" ";
        index[0]++;
        delete[] A;
    }
    return 0;
}



3个低级排序之中,bubble最慢了!

插入排序


  1. //time:10.920s  
  2. #include<cstring>  
  3. #include<iostream>  
  4. #include<fstream>  
  5. #include<algorithm>  
  6. using namespace std;  
  7. class MY{  
  8.     int a;  
  9. //public:  
  10.     friend ostream& operator <<(ostream& os,const MY &my){  
  11.         os<<my.a;return os;  
  12.     }  
  13.     friend istream& operator >>(istream& is,MY& my){  
  14.         is>>my.a;return is;  
  15.     }  
  16. public:  
  17.     bool operator<(const MY& another){  
  18.         return this->a<another.a;  
  19.     }  
  20. };  
  21. template<class T>  
  22. void insertion(T A[],int n)  
  23. {  
  24.     //A[0...i-1]是非递减序的  
  25.     for(int i=1;i<n;i++){  
  26.         T key=A[i];  
  27.         int j=i-1;  
  28.         //A[j+2...i]是非递减序的,且都大于key  
  29.         while(j>=0&&key<A[j]){A[j+1]=A[j];j--;}  
  30.         A[j+1]=key;  
  31.     }  
  32. }  
  33. template<typename T>  
  34. bool check_if_sorted(T A[],int n)  
  35. {  
  36.     for(int i=1;i<n;i++)  
  37.         if(A[i]<A[i-1])return false;  
  38.     return true;  
  39. }  
  40. int main()  
  41. {  
  42.     //测试数据:  
  43.     //输入格式:第一行是n,元素个数。第二行是n个元素。  
  44.     //0.txt 无元素  
  45.     //1.txt 1个元素  
  46.     //2.txt 2个元素  
  47.     //3.txt 5个元素  
  48.     //4.txt 均为相同元素  
  49.     //5.txt 大量相同元素  
  50.     //6.txt 随机元素  
  51.     //7.txt 逆序元素  
  52.     //8.txt 升序元素  
  53.     //9.txt 1000000个元素  
  54.     //有a后缀即为排序后的输出  
  55.     char index[10]="0";  
  56.     for(int i=0;i<9;i++){  
  57.         char file[10];strcpy(file,index);  
  58.         strcat(file,".txt");  
  59.         ifstream fin(file);  
  60.         if(!fin){cerr<<"can't open input file \""<<file<<"\""<<endl;exit(EXIT_FAILURE);}  
  61.         int n;fin>>n;  
  62.         MY* A=new MY [n];  
  63.         for(int i=0;i<n;i++) fin>>A[i];  
  64.         insertion(A,n);  
  65.         cout<<check_if_sorted(A,n)<<endl;  
  66.         char filea[10];strcpy(filea,index);  
  67.         strcat(filea,"a.txt");  
  68.         ofstream fout(filea);  
  69.         for(int i=0;i<n;i++) fout<<A[i]<<" ";  
  70.         index[0]++;  
  71.         delete[] A;  
  72.     }  
  73.     return 0;  
  74. }  

选择排序


  1. //time:18.650s  
  2. #include<cstring>  
  3. #include<iostream>  
  4. #include<fstream>  
  5. #include<algorithm>  
  6. using namespace std;  
  7. class MY{//设置此class只是为了C++练手  
  8.     int a;  
  9. //public:  
  10.     friend ostream& operator <<(ostream& os,const MY &my){  
  11.         os<<my.a;return os;  
  12.     }  
  13.     friend istream& operator >>(istream& is,MY& my){  
  14.         is>>my.a;return is;  
  15.     }  
  16. public:  
  17.     bool operator<(const MY& another){  
  18.         return this->a<another.a;  
  19.     }  
  20. };  
  21. template<class T>  
  22. void selection(T A[],int n)  
  23. {  
  24.     for(int i=0;i<n-1;i++){  
  25.         T min=A[i];int min_index=i;  
  26.         for(int j=i+1;j<n;j++)  
  27.             if(A[j]<min){min=A[j];min_index=j;}  
  28.         swap(A[i],A[min_index]);  
  29.     }  
  30. }  
  31. template<typename T>  
  32. bool check_if_sorted(T A[],int n)  
  33. {  
  34.     for(int i=1;i<n;i++)  
  35.         if(A[i]<A[i-1])return false;  
  36.     return true;  
  37. }  
  38. int main()  
  39. {  
  40.     //测试数据:  
  41.     //输入格式:第一行是n,元素个数。第二行是n个元素。  
  42.     //0.txt 无元素  
  43.     //1.txt 1个元素  
  44.     //2.txt 2个元素  
  45.     //3.txt 5个元素  
  46.     //4.txt 均为相同元素  
  47.     //5.txt 大量相同元素  
  48.     //6.txt 随机元素  
  49.     //7.txt 逆序元素  
  50.     //8.txt 升序元素  
  51.     //9.txt 1000000个元素  
  52.     //有a后缀即为排序后的输出  
  53.     char index[10]="0";  
  54.     for(int i=0;i<9;i++){  
  55.         char file[10];strcpy(file,index);  
  56.         strcat(file,".txt");  
  57.         ifstream fin(file);  
  58.         if(!fin){cerr<<"can't open input file \""<<file<<"\""<<endl;exit(EXIT_FAILURE);}  
  59.         int n;fin>>n;  
  60.         MY* A=new MY [n];  
  61.         for(int i=0;i<n;i++) fin>>A[i];  
  62.         selection(A,n);  
  63.         cout<<check_if_sorted(A,n)<<endl;  
  64.         char filea[10];strcpy(filea,index);  
  65.         strcat(filea,"a.txt");  
  66.         ofstream fout(filea);  
  67.         for(int i=0;i<n;i++) fout<<A[i]<<" ";  
  68.         index[0]++;  
  69.         delete[] A;  
  70.     }  
  71.     return 0;  
  72. }  



堆排序


  1. //time:2.460s  
  2. #include<cstring>  
  3. #include<iostream>  
  4. #include<fstream>  
  5. #include<algorithm>  
  6. using namespace std;  
  7. class MY{  
  8.     int a;  
  9. //public:  
  10.     friend ostream& operator <<(ostream& os,const MY &my){  
  11.         os<<my.a;return os;  
  12.     }  
  13.     friend istream& operator >>(istream& is,MY& my){  
  14.         is>>my.a;return is;  
  15.     }  
  16. public:  
  17.     bool operator<(const MY& another){  
  18.         return this->a<another.a;  
  19.     }  
  20. };  
  21. int RANDOM(int begin,int end)  
  22. {//[begin,end]  
  23.     int seed=rand()*(RAND_MAX+1)+rand();  
  24.     return seed%(end-begin+1)+begin;  
  25. }  
  26. template<class T>  
  27. void fixdown(T A[],int parent,int n)  
  28. {//A[parent]元素是以A[parent...n]为大顶堆的唯一违反。  
  29.   
  30.     while(true){  
  31.         int left=(parent<<1),right=left+1;  
  32.         int max=parent;  
  33.         if(left<=n&&A[max]<A[left]) max=left;  
  34.         if(right<=n&&A[max]<A[right]) max=right;  
  35.         if(max==parent) break;  
  36.         swap(A[parent],A[max]);parent=max;  
  37.     }  
  38. }  
  39. template<class T>  
  40. void heapsort(T A[],int n)  
  41. {  
  42.     for(int i=n>>1;i>=1;i--) fixdown(A,i,n);  
  43.     //for(int i=1;i<=n;i++) printf("%d ",A[i]);putchar('\n');  
  44.     while(n>1){  
  45.         swap(A[1],A[n]);fixdown(A,1,--n);  
  46.     }  
  47. }  
  48. template<typename T>  
  49. bool check_if_sorted(T A[],int n)  
  50. {  
  51.     for(int i=1;i<n;i++)  
  52.         if(A[i+1]<A[i])return false;  
  53.     return true;  
  54. }  
  55. int main()  
  56. {  
  57.     //测试数据:  
  58.     //输入格式:第一行是n,元素个数。第二行是n个元素。  
  59.     //0.txt 无元素  
  60.     //1.txt 1个元素  
  61.     //2.txt 2个元素  
  62.     //3.txt 5个元素  
  63.     //4.txt 均为相同元素  
  64.     //5.txt 大量相同元素  
  65.     //6.txt 随机元素  
  66.     //7.txt 逆序元素  
  67.     //8.txt 升序元素  
  68.     //9.txt 1000000个元素  
  69.     //有a后缀的文件即为排序后的输出  
  70.     char index[10]="0";  
  71.     for(int i=0;i<10;i++){  
  72.         char file[10];strcpy(file,index);  
  73.         strcat(file,".txt");  
  74.         ifstream fin(file);  
  75.         if(!fin){cerr<<"can't open input file \""<<file<<"\""<<endl;exit(EXIT_FAILURE);}  
  76.         int n;fin>>n;  
  77.         MY*A=new MY [n+1];//堆排序下标是1~n  
  78.         for(int i=1;i<=n;i++) fin>>A[i];  
  79.         heapsort(A,n);  
  80.         cout<<check_if_sorted(A,n)<<endl;  
  81.         char filea[10];strcpy(filea,index);  
  82.         strcat(filea,"a.txt");  
  83.         ofstream fout(filea);  
  84.         for(int i=1;i<=n;i++) fout<<A[i]<<" ";  
  85.         index[0]++;  
  86.         delete[] A;  
  87.     }  
  88.     return 0;  
  89. }  



使用非递归的归并排序



  1. //time:3.400s  
  2. //使用非递归的归并排序  
  3. #include<cstring>  
  4. #include<iostream>  
  5. #include<fstream>  
  6. #include<algorithm>  
  7. using namespace std;  
  8. class MY{  
  9.     int a;  
  10. //public:  
  11.     friend ostream& operator <<(ostream& os,const MY &my){  
  12.         os<<my.a;return os;  
  13.     }  
  14.     friend istream& operator >>(istream& is,MY& my){  
  15.         is>>my.a;return is;  
  16.     }  
  17. public:  
  18.     bool operator<(const MY& another){  
  19.         return this->a<another.a;  
  20.     }  
  21. };  
  22. int RANDOM(int begin,int end)  
  23. {//[begin,end]  
  24.     int seed=rand()*(RAND_MAX+1)+rand();  
  25.     return seed%(end-begin+1)+begin;  
  26. }  
  27. template<class T>  
  28. int merge(T A[],int left,int middle,int right)  
  29. {//归并两个有序数组A[left...middle]和A[middle+1...right]  
  30.     int len1=middle-left+1,len2=right-middle;  
  31.     T* Aux1=new T[len1],*Aux2=new T[len2];  
  32.     for(int i=left;i<=middle;i++) Aux1[i-left]=A[i];  
  33.     for(int j=middle+1;j<=right;j++) Aux2[j-middle-1]=A[j];  
  34.     int i=0,j=0,k=0;  
  35.     while(i<len1&&j<len2){  
  36.         if(Aux1[i]<Aux2[j]) A[left+k++]=Aux1[i++];  
  37.         else A[left+k++]=Aux2[j++];  
  38.     }  
  39.     while(i<len1) A[left+k++]=Aux1[i++];  
  40.     while(j<len2) A[left+k++]=Aux2[j++];  
  41.     delete[] Aux1;delete[] Aux2;  
  42. }  
  43. template<class T>  
  44. void bottom_up_mergesort(T A[],int n)  
  45. {  
  46.     for(int len=1;len<=n;len<<=1)  
  47.         for(int i=0;i<n-len;i+=(len<<1))//由i+len-1<n-1得i<n-len。最后一趟合并,一边是len长,另一边是剩下的长。i+len-1<n-1是保证一边有len长,且剩下有至少1的长度  
  48.             merge(A,i,i+len-1,min(i+(len<<1)-1,n-1));  
  49. }  
  50. template<typename T>  
  51. bool check_if_sorted(T A[],int n)  
  52. {  
  53.     for(int i=1;i<n;i++)  
  54.         if(A[i]<A[i-1])return false;  
  55.     return true;  
  56. }  
  57. int main()  
  58. {  
  59.     //测试数据:  
  60.     //输入格式:第一行是n,元素个数。第二行是n个元素。  
  61.     //0.txt 无元素  
  62.     //1.txt 1个元素  
  63.     //2.txt 2个元素  
  64.     //3.txt 5个元素  
  65.     //4.txt 均为相同元素  
  66.     //5.txt 大量相同元素  
  67.     //6.txt 随机元素  
  68.     //7.txt 逆序元素  
  69.     //8.txt 升序元素  
  70.     //9.txt 1000000个元素  
  71.     //有a后缀的文件即为排序后的输出  
  72.     char index[10]="0";  
  73.     for(int i=0;i<10;i++){  
  74.         char file[10];strcpy(file,index);  
  75.         strcat(file,".txt");  
  76.         ifstream fin(file);  
  77.         if(!fin){cerr<<"can't open input file \""<<file<<"\""<<endl;exit(EXIT_FAILURE);}  
  78.         int n;fin>>n;  
  79.         MY*A=new MY [n];  
  80.         for(int i=0;i<n;i++) fin>>A[i];  
  81.         bottom_up_mergesort(A,n);  
  82.         cout<<check_if_sorted(A,n)<<endl;  
  83.         char filea[10];strcpy(filea,index);  
  84.         strcat(filea,"a.txt");  
  85.         ofstream fout(filea);  
  86.         for(int i=0;i<n;i++) fout<<A[i]<<" ";  
  87.         index[0]++;  
  88.         delete[] A;  
  89.     }  
  90.     return 0;  




使用递归的归并排序



  1. //3.370s  
  2. //使用递归的归并排序  
  3. #include<cstring>  
  4. #include<iostream>  
  5. #include<fstream>  
  6. #include<algorithm>  
  7. using namespace std;  
  8. class MY{  
  9.     int a;  
  10. //public:  
  11.     friend ostream& operator <<(ostream& os,const MY &my){  
  12.         os<<my.a;return os;  
  13.     }  
  14.     friend istream& operator >>(istream& is,MY& my){  
  15.         is>>my.a;return is;  
  16.     }  
  17. public:  
  18.     bool operator<(const MY& another){  
  19.         return this->a<another.a;  
  20.     }  
  21. };  
  22. int RANDOM(int begin,int end)  
  23. {//[begin,end]  
  24.     int seed=rand()*(RAND_MAX+1)+rand();  
  25.     return seed%(end-begin+1)+begin;  
  26. }  
  27. template<class T>  
  28. int merge(T A[],int left,int middle,int right)  
  29. {//归并两个有序数组A[left...middle]和A[middle+1...right]  
  30.     int len1=middle-left+1,len2=right-middle;  
  31.     T* Aux1=new T[len1],*Aux2=new T[len2];  
  32.     for(int i=left;i<=middle;i++) Aux1[i-left]=A[i];  
  33.     for(int j=middle+1;j<=right;j++) Aux2[j-middle-1]=A[j];  
  34.     int i=0,j=0,k=0;  
  35.     while(i<len1&&j<len2){  
  36.         if(Aux1[i]<Aux2[j]) A[left+k++]=Aux1[i++];  
  37.         else A[left+k++]=Aux2[j++];  
  38.     }  
  39.     while(i<len1) A[left+k++]=Aux1[i++];  
  40.     while(j<len2) A[left+k++]=Aux2[j++];  
  41.     delete[] Aux1;delete[] Aux2;  
  42. }  
  43. template<class T>  
  44. void mergesort(T A[],int p,int q)  
  45. {  
  46.     if(p>=q) return ;  
  47.     int middle=(p+q)/2;  
  48.     mergesort(A,p,middle);  
  49.     mergesort(A,middle+1,q);  
  50.     merge(A,p,middle,q);  
  51. }  
  52. template<typename T>  
  53. bool check_if_sorted(T A[],int n)  
  54. {  
  55.     for(int i=1;i<n;i++)  
  56.         if(A[i]<A[i-1])return false;  
  57.     return true;  
  58. }  
  59. int main()  
  60. {  
  61.     //测试数据:  
  62.     //输入格式:第一行是n,元素个数。第二行是n个元素。  
  63.     //0.txt 无元素  
  64.     //1.txt 1个元素  
  65.     //2.txt 2个元素  
  66.     //3.txt 5个元素  
  67.     //4.txt 均为相同元素  
  68.     //5.txt 大量相同元素  
  69.     //6.txt 随机元素  
  70.     //7.txt 逆序元素  
  71.     //8.txt 升序元素  
  72.     //9.txt 1000000个元素  
  73.     //有a后缀的文件即为排序后的输出  
  74.     char index[10]="0";  
  75.     for(int i=0;i<10;i++){  
  76.         char file[10];strcpy(file,index);  
  77.         strcat(file,".txt");  
  78.         ifstream fin(file);  
  79.         if(!fin){cerr<<"can't open input file \""<<file<<"\""<<endl;exit(EXIT_FAILURE);}  
  80.         int n;fin>>n;  
  81.         MY*A=new MY [n];  
  82.         for(int i=0;i<n;i++) fin>>A[i];  
  83.         mergesort(A,0,n-1);  
  84.         cout<<check_if_sorted(A,n)<<endl;  
  85.         char filea[10];strcpy(filea,index);  
  86.         strcat(filea,"a.txt");  
  87.         ofstream fout(filea);  
  88.         for(int i=0;i<n;i++) fout<<A[i]<<" ";  
  89.         index[0]++;  
  90.         delete[] A;  
  91.     }  
  92.     return 0;  



随机化的快速排序



  1. //time:3.770s  
  2. //随机化的快速排序。若不使用随机化的话,遇到极端情况,升序或逆序排列,就会递归深度过大,stack overflow。  
  3. #include<cstring>  
  4. #include<iostream>  
  5. #include<fstream>  
  6. #include<algorithm>  
  7. using namespace std;  
  8. class MY{  
  9.     int a;  
  10. //public:  
  11.     friend ostream& operator <<(ostream& os,const MY &my){  
  12.         os<<my.a;return os;  
  13.     }  
  14.     friend istream& operator >>(istream& is,MY& my){  
  15.         is>>my.a;return is;  
  16.     }  
  17. public:  
  18.     bool operator<(const MY& another){  
  19.         return this->a<another.a;  
  20.     }  
  21. };  
  22. int RANDOM(int begin,int end)  
  23. {//[begin,end]  
  24.     int seed=rand()*(RAND_MAX+1)+rand();  
  25.     return seed%(end-begin+1)+begin;  
  26. }  
  27. template<class T>  
  28. int partition(T A[],int p,int q)  
  29. {  
  30.     int pivot=p-1;T key=A[q];  
  31.     //A[p...pivot] < key  
  32.     //A[pivot+1...j-1] >= key  
  33.     //A[q]         = key  
  34.     for(int j=p;j<q;j++)  
  35.         if(A[j]<key) swap(A[j],A[++pivot]);  
  36.     swap(A[pivot+1],A[q]);  
  37.     return pivot+1;  
  38. }  
  39. template<class T>  
  40. void quicksort(T A[],int p,int q)  
  41. {  
  42.     if(p>=q) return ;  
  43.     swap(A[q],A[RANDOM(p,q)]);//随机选择划分主元  
  44.     int pivot=partition(A,p,q);  
  45.     quicksort(A,p,pivot-1);  
  46.     quicksort(A,pivot+1,q);  
  47. }  
  48. template<typename T>  
  49. bool check_if_sorted(T A[],int n)  
  50. {  
  51.     for(int i=1;i<n;i++)  
  52.         if(A[i]<A[i-1])return false;  
  53.     return true;  
  54. }  
  55. int main()  
  56. {  
  57.     //测试数据:  
  58.     //输入格式:第一行是n,元素个数。第二行是n个元素。  
  59.     //0.txt 无元素  
  60.     //1.txt 1个元素  
  61.     //2.txt 2个元素  
  62.     //3.txt 5个元素  
  63.     //4.txt 均为相同元素  
  64.     //5.txt 大量相同元素  
  65.     //6.txt 随机元素  
  66.     //7.txt 逆序元素  
  67.     //8.txt 升序元素  
  68.     //9.txt 1000000个元素  
  69.     //有a后缀的文件即为排序后的输出  
  70.     char index[10]="0";  
  71.     for(int i=0;i<10;i++){  
  72.         char file[10];strcpy(file,index);  
  73.         strcat(file,".txt");  
  74.         ifstream fin(file);  
  75.         if(!fin){cerr<<"can't open input file \""<<file<<"\""<<endl;exit(EXIT_FAILURE);}  
  76.         int n;fin>>n;  
  77.         MY*A=new MY [n];  
  78.         for(int i=0;i<n;i++) fin>>A[i];  
  79.         quicksort(A,0,n-1);  
  80.         cout<<check_if_sorted(A,n)<<endl;  
  81.         char filea[10];strcpy(filea,index);  
  82.         strcat(filea,"a.txt");  
  83.         ofstream fout(filea);  
  84.         for(int i=0;i<n;i++) fout<<A[i]<<" ";  
  85.         index[0]++;  
  86.         delete[] A;  
  87.     }  
  88.     return 0;  
  89. }  
前3个低级排序之所以没有用9.txt测试,是因为n^2太慢了,跑不起。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值