C++中几种排序算法的实现

129 阅读3分钟

其实问题很简单:随机生成100个数,编写以下4种排序算法对其从小到大排序。

  • 冒泡排序
  • 快速排序
  • 希尔排序
  • 堆排序
  • 归并排序
  • 选择排序
  • 冒泡排序

\

算法理解:(具体参考:www.cnblogs.com/kkun/archiv…

代码实现:

[cpp]  view plain  copy

  1. #include <iostream>  
  2. #include <cstdlib>  
  3. #include <ctime>  
  4.   
  5. using namespace std;  
  6. //Swap-交换  
  7. template<class T>  
  8. void Swap(T &a,T &b)  
  9. {  
  10.     T tmp=a;  
  11.     a=b;  
  12.     b=tmp;  
  13. }  
  14. //BubbleSorting Small->Large  
  15. template<class T>  
  16. void BubbleSorting(T A[])  
  17. {  
  18.     for(int i=99;i>0;i--)//Times-外循环,n-1次  
  19.     {  
  20.         int flag=0;//引入flag,稍作性能提升  
  21.         for(int j=0;j<99;j++)  
  22.         {  
  23.             if(A[j]>A[j+1])  
  24.             {  
  25.                 Swap(A[j],A[j+1]);  
  26.                 flag=1;  
  27.             }  
  28.         }  
  29.         if(!flag)break;  
  30.     }  
  31. }  
  32. int main()  
  33. {  
  34.     clock_t start_time = clock();  
  35.   
  36.     srand(unsigned(time(NULL)));  
  37.     const int min=-100;  
  38.     const int max= 100;  
  39.     int A[200];  
  40.     for(int i=0;i<100;i++){A[i]=rand()%(max-min+1)+min;}//Generate 100 random figures  
  41.     cout<<"The random numbers are:"<<endl;  
  42.     for(int i=0;i<100;i++){cout<<A[i]<<"\t";}//Output the disordered numbers  
  43.     cout<<endl;  
  44.     cout<<"The ordered numbers are:"<<endl;  
  45.     BubbleSorting(A);//Bubble Sorting  
  46.     for(int i=0;i<100;i++){cout<<A[i]<<"\t";}//Output the disordered numbers  
  47.     cout<<endl;  
  48.     cout << "The elapsed time is:" << double(clock() - start_time) << 's' << endl;  
  49.     return 0;  
  50. }  

\

  • 快速排序

\

算法理解:(具体参考:www.cnblogs.com/MOBIN/p/468…

代码实现:

[cpp]  view plain  copy

  1.   

[cpp]  view plain  copy

  1. #include<iostream>  
  2. #include<cstdlib>  
  3. #include<ctime>  
  4.   
  5. using namespace std;  
  6. //交换   
  7. template<class T>  
  8. void Swap(T &a,T &b)  
  9. {  
  10.     T tmp=a;  
  11.     a=b;  
  12.     b=tmp;  
  13. }  
  14. //划分  
  15. template<class T>  
  16. int Partion(T elem[],int low,int high)  
  17. {  
  18.     while(low<high)//这里无需“=”即可  
  19.     {  
  20.         while(low<high&&elem[low]<=elem[high])high--;  
  21.         Swap(elem[low],elem[high]);  
  22.         while(low<high&&elem[low]<=elem[high])low++;  
  23.         Swap(elem[low],elem[high]);  
  24.     }  
  25.     return low;  
  26. }  
  27. //递归   
  28. template<class T>  
  29. void Help(T elem[],int low,int high)  
  30. {  
  31.     if(low<high)  
  32.     {  
  33.         int KeyPoint=Partion(elem,low,high);  
  34.         Help(elem,low,KeyPoint-1);  
  35.         Help(elem,KeyPoint+1,high);  
  36.     }  
  37. }  
  38. template<class T>  
  39. void QuickSort(T elem[],int n)  
  40. {  
  41.     Help(elem,0,n-1);  
  42. }  
  43.   
  44. int main()  
  45. {  
  46.     clock_t start_time=clock();  
  47.     srand(unsigned(time(NULL)));  
  48.   
  49.     const int min=-100;  
  50.     const int max= 100;  
  51.     int A[200];  
  52.     cout<<"The random numbers are:"<<endl;  
  53.     for(int i=0;i<100;i++)A[i]=rand()%(max-min+1)+min;  
  54.     for(int i=0;i<100;i++)cout<<A[i]<<"\t";  
  55.     cout<<endl;  
  56.     cout<<"The ordered numbers are:"<<endl;  
  57.     QuickSort(A,100);  
  58.     for(int i=0;i<100;i++)cout<<A[i]<<"\t";  
  59.     cout<<endl;  
  60.     cout<<"The elapsed time is "<< double(clock()-start_time) <<'s'<<endl;  
  61.   
  62. }  



\

  • 希尔排序

\

算法理解: (具体参考:www.cnblogs.com/skywang1234…

代码实现:

[cpp]  view plain  copy

  1. #include <iostream>  
  2. #include <cstdlib>  
  3. #include <ctime>  
  4. using namespace std;  
  5.   
  6. template<class T>  
  7. void ShellInsert(T elem[],int n,int incr)<span style="font-family:Arial, Helvetica, sans-serif;">//incr代表间距</span>  
  8. {  
  9.     for(int i=incr;i<n;i++)  
  10.     {  
  11.         T xp=elem[i];  
  12.         int j;  
  13.         for(j=i-incr;j>=0&&elem[j]<xp;j-=incr)//注意这里是xp,若写elem[i],数值容易被覆盖,导致错误排序。  
  14.         {  
  15.             elem[j+incr]=elem[j];  
  16.         }  
  17.         elem[j+incr]=xp;  
  18.     }  
  19. }  
  20. template<class T>  
  21. void ShellSorting(T elem[],int n,int inc[],int t)  
  22. {  
  23.     for(int k=0;k<t;k++)  
  24.     {  
  25.         ShellInsert(elem,n,inc[k]);  
  26.     }  
  27. }  
  28. int main()  
  29. {  
  30.     clock_t start_time=clock();  
  31.   
  32.     srand(unsigned(time(NULL)));  
  33.     const int min=-300;  
  34.     const int max= 300;  
  35.     int A[100];  
  36.     int inc[10];  
  37.     int num=100;  
  38.     for(int i=0;i<6;i++)  
  39.     {  
  40.         num=num/2;  
  41.         inc[i]=num;  
  42.     }  
  43.   
  44.     cout<<"The random numbers are:"<<endl;  
  45.     for(int i=0;i<100;i++)A[i]=rand()%(max-min+1)+min;  
  46.     for(int i=0;i<100;i++)cout<<A[i]<<"\t";  
  47.     cout<<endl;  
  48.     cout<<"The ordered numbers are:"<<endl;  
  49.     ShellSorting(A,100,inc,6);  
  50.     for(int i=0;i<100;i++)cout<<A[i]<<"\t";  
  51.     cout<<endl;  
  52.     cout<<"The elapsed time is: "<<double(clock()-start_time)<<'s'<<endl;  
  53.   
  54.     return 0;  
  55. }  

\

  • 堆排序

\

算法理解: (具体参考:www.cnblogs.com/jingmoxukon…

代码实现:

[cpp]  view plain  copy

  1. #include <iostream>  

  2. #include <cstdlib>  

  3. #include <ctime>  

  4. using namespace std;  

  5.   

  6. //Swap  

  7. void Swap(int &a,int &b){  

  8.     int tmp=a;  

  9.     a=b;  

  10.     b=tmp;  

  11. }  

  12. //Adjust the big node  

  13. void AdjustHelp(int A[],int low,int high){

    //调整堆  

  14.     for(int p=low,i=low*2+1;i<=high;i=i*2+1){  

  15.         if(i<high&&A[i]<A[i+1])i++;  

  16.         if(A[p]>A[i])break;  

  17.         Swap(A[p],A[i]);  

  18.         p=i;  

  19.     }  

  20. }  

  21. //HeapSort Algorithm  

  22. void HeapSort(int A[],int n){  

  23.     for(int i=(n-2)/2;i>=0;i--){

    //写入堆  

  24.         AdjustHelp(A,i,n-1);  

  25.     }  

  26.     for(int i=n-1;i>0;i--){

    //排序  

  27.         Swap(A[i],A[0]);  

  28.         AdjustHelp(A,0,i-1);  

  29.     }  

  30. }  

  31. int main()  

  32. {  

  33.     clock_t start_time = clock();  

  34.     int A[100];  

  35.     const int min=-99;  

  36.     const int max=99;  

  37.     srand(unsigned(time(NULL)));  

  38.     cout << "The previous numbers are:" << endl;  

  39.     for(int i=0;i<100;i++)A[i]=rand()%(max-min+1)+min;  

  40.     for(int i=0;i<100;i++)cout<<A[i]<<"\t";  

  41.     cout << endl;  

  42.     cout << "The current numbers are:"<<endl;  

  43.     HeapSort(A,100);  

  44.     for(int i=0;i<100;i++)cout<<A[i]<<"\t";  

  45.     cout << endl;  

  46.     cout << "The elapsed time is: " << double(clock()-start_time)<<"s"<<endl;  

  47.     return 0;  

}

  • 归并排序

算法理解: (具体参考:blog.csdn.net/yuehailin/a…

代码实现:

[cpp]  view plain  copy

  1. #include <iostream>  
  2. #include <cstdlib>  
  3. #include <ctime>  
  4. using namespace std;  
  5. void Merge(int elem[],int tmp[],int low,int mid,int high){  
  6.     int i=low,j=mid+1;  
  7.     int m=mid,n=high;  
  8.     int k=low;  
  9.     while(i<=m&&j<=n){  
  10.         if(elem[i]<elem[j]){  
  11.             tmp[k++]=elem[i++];  
  12.         }  
  13.         else  
  14.             tmp[k++]=elem[j++];  
  15.     }  
  16.       
  17.     while(i<=m)tmp[k++]=elem[i++];  
  18.     while(j<=n)tmp[k++]=elem[j++];  
  19.     for(i=low;i<=high;i++){  
  20.         elem[i]=tmp[i];  
  21.     }  
  22. }  
  23. //  
  24. void Help(int elem[],int tmp[],int low,int high){  
  25.     if(low<high){  
  26.         int mid=(low+high)/2;  
  27.         Help(elem, tmp, low, mid);  
  28.         Help(elem,tmp,mid+1,high);  
  29.         Merge(elem, tmp, low, mid, high);  
  30.     }  
  31. }  
  32. //  
  33. void MergeSort(int elem[],int n){  
  34.     int *p=new int[n];  
  35.     Help(elem,p,0,n-1);  
  36.     delete[] p;  
  37. }  
  38. int main() {  
  39.     // insert code here...  
  40.     clock_t start_time=clock();  
  41.     srand(unsigned(time(NULL)));  
  42.     int A[100];  
  43.     const int min=1;  
  44.     const int max=100;  
  45.     cout<<"The previous numbers are:"<<endl;  
  46.     for(int i=0;i<100;i++)A[i]=rand()%(max-min+1)+min;  
  47.     for(int i=0;i<100;i++)cout<<A[i]<<"\t";  
  48.     cout<<endl;  
  49.     cout<<"The current numbers are:"<<endl;  
  50.     MergeSort(A,100);  
  51.     for(int i=0;i<100;i++)cout<<A[i]<<"\t";  
  52.     cout<<endl;  
  53.     cout<<"The elapsed time is: "<<double(clock()-start_time)<<"s"<<endl;  
  54.     cout<<endl;  
  55.     return 0;  
  56. }  
  • 选择排序(C)

\

\

代码实现:

[cpp]  view plain  copy

  1. #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    int main()
    {


    time_t ts;
    srand((unsigned int)time(&ts));
    int a[10];
    printf("随机数为:");
    for(int i=0; i<10; i++)
    {


    a[i]=rand()%100;
    printf("%d ",a[i]);
    }

    for(int i=0; i<9; i++) //0-8 9个数 最后一个数不用比较
    {


    int max=i;//标识最大数下标 假定a[0]最大
    for(int j=i+1; j<10; j++) //选择法排序
    {


    if(a[max]<a[j])
    {


    max=j;
    }
    }
    if(max!=i)
    {


    int t=a[max];
    a[max]=a[i];
    a[i]=t;
    }
    }
    printf("\n从大到小排序为:");
    for(int i=0; i<10; i++)
    {


    printf("%d ",a[i]);
    }
    return 0;
    }