随机生成100个数,利用几种排序算法对其实现排序(C++)

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

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

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

以下内容主要包括算法实现。

  • 补充内容



概念内容:

#include<iostream>:主要功能是处理输入输出流。

(具体参考:https://baike.baidu.com/item/iostream/2850567?fr=aladdin

#include<cstdlib>:集成了一些常用的函数。

(具体参考:http://blog.csdn.net/xiao229404041/article/details/6721192

#include<ctime>:把日期和时间转换为字符串。
(具体参考:http://blog.csdn.net/l773575310/article/details/53258230
namespace:与作用域相关,主要用于命名空间。
(具体参考:http://blog.csdn.net/yao_zhuang/article/details/1853625

  • 冒泡排序



算法理解:(具体参考:https://www.cnblogs.com/kkun/archive/2011/11/23/2260280.html
代码实现:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
//Swap-交换
template<class T>
void Swap(T &a,T &b)
{
    T tmp=a;
    a=b;
    b=tmp;
}
//BubbleSorting Small->Large
template<class T>
void BubbleSorting(T A[])
{
    for(int i=99;i>0;i--)//Times-外循环,n-1次
    {
        int flag=0;//引入flag,稍作性能提升
        for(int j=0;j<99;j++)
        {
            if(A[j]>A[j+1])
            {
                Swap(A[j],A[j+1]);
                flag=1;
            }
        }
        if(!flag)break;
    }
}
int main()
{
    clock_t start_time = clock();

    srand(unsigned(time(NULL)));
    const int min=-100;
    const int max= 100;
    int A[200];
    for(int i=0;i<100;i++){A[i]=rand()%(max-min+1)+min;}//Generate 100 random figures
    cout<<"The random numbers are:"<<endl;
    for(int i=0;i<100;i++){cout<<A[i]<<"\t";}//Output the disordered numbers
    cout<<endl;
    cout<<"The ordered numbers are:"<<endl;
    BubbleSorting(A);//Bubble Sorting
    for(int i=0;i<100;i++){cout<<A[i]<<"\t";}//Output the disordered numbers
    cout<<endl;
    cout << "The elapsed time is:" << double(clock() - start_time) << 's' << endl;
    return 0;
}

  • 快速排序



算法理解:(具体参考:https://www.cnblogs.com/MOBIN/p/4681369.html
代码实现

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;
//交换 
template<class T>
void Swap(T &a,T &b)
{
    T tmp=a;
    a=b;
    b=tmp;
}
//划分
template<class T>
int Partion(T elem[],int low,int high)
{
    while(low<high)//这里无需“=”即可
    {
        while(low<high&&elem[low]<=elem[high])high--;
        Swap(elem[low],elem[high]);
        while(low<high&&elem[low]<=elem[high])low++;
        Swap(elem[low],elem[high]);
    }
    return low;
}
//递归 
template<class T>
void Help(T elem[],int low,int high)
{
    if(low<high)
    {
        int KeyPoint=Partion(elem,low,high);
        Help(elem,low,KeyPoint-1);
        Help(elem,KeyPoint+1,high);
    }
}
template<class T>
void QuickSort(T elem[],int n)
{
    Help(elem,0,n-1);
}

int main()
{
    clock_t start_time=clock();
    srand(unsigned(time(NULL)));

    const int min=-100;
    const int max= 100;
    int A[200];
    cout<<"The random numbers are:"<<endl;
    for(int i=0;i<100;i++)A[i]=rand()%(max-min+1)+min;
    for(int i=0;i<100;i++)cout<<A[i]<<"\t";
    cout<<endl;
    cout<<"The ordered numbers are:"<<endl;
    QuickSort(A,100);
    for(int i=0;i<100;i++)cout<<A[i]<<"\t";
    cout<<endl;
    cout<<"The elapsed time is "<< double(clock()-start_time) <<'s'<<endl;

}

  • 希尔排序



算法理解:
(具体参考:https://www.cnblogs.com/skywang12345/p/3597597.html
代码实现:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

template<class T>
void ShellInsert(T elem[],int n,int incr)//incr代表间距
{
    for(int i=incr;i<n;i++)
    {
        T xp=elem[i];
        int j;
        for(j=i-incr;j>=0&&elem[j]<xp;j-=incr)//注意这里是xp,若写elem[i],数值容易被覆盖,导致错误排序。
        {
            elem[j+incr]=elem[j];
        }
        elem[j+incr]=xp;
    }
}
template<class T>
void ShellSorting(T elem[],int n,int inc[],int t)
{
    for(int k=0;k<t;k++)
    {
        ShellInsert(elem,n,inc[k]);
    }
}
int main()
{
    clock_t start_time=clock();

    srand(unsigned(time(NULL)));
    const int min=-300;
    const int max= 300;
    int A[100];
    int inc[10];
    int num=100;
    for(int i=0;i<6;i++)
    {
        num=num/2;
        inc[i]=num;
    }

    cout<<"The random numbers are:"<<endl;
    for(int i=0;i<100;i++)A[i]=rand()%(max-min+1)+min;
    for(int i=0;i<100;i++)cout<<A[i]<<"\t";
    cout<<endl;
    cout<<"The ordered numbers are:"<<endl;
    ShellSorting(A,100,inc,6);
    for(int i=0;i<100;i++)cout<<A[i]<<"\t";
    cout<<endl;
    cout<<"The elapsed time is: "<<double(clock()-start_time)<<'s'<<endl;

    return 0;
}

  • 堆排序



算法理解:
(具体参考:https://www.cnblogs.com/jingmoxukong/p/4303826.html
代码实现:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

//Swap
void Swap(int &a,int &b){
    int tmp=a;
    a=b;
    b=tmp;
}
//Adjust the big node
void AdjustHelp(int A[],int low,int high){//调整堆
    for(int p=low,i=low*2+1;i<=high;i=i*2+1){
        if(i<high&&A[i]<A[i+1])i++;
        if(A[p]>A[i])break;
        Swap(A[p],A[i]);
        p=i;
    }
}
//HeapSort Algorithm
void HeapSort(int A[],int n){
    for(int i=(n-2)/2;i>=0;i--){//写入堆
        AdjustHelp(A,i,n-1);
    }
    for(int i=n-1;i>0;i--){//排序
        Swap(A[i],A[0]);
        AdjustHelp(A,0,i-1);
    }
}
int main()
{
    clock_t start_time = clock();
    int A[100];
    const int min=-99;
    const int max=99;
    srand(unsigned(time(NULL)));
    cout << "The previous numbers are:" << endl;
    for(int i=0;i<100;i++)A[i]=rand()%(max-min+1)+min;
    for(int i=0;i<100;i++)cout<<A[i]<<"\t";
    cout << endl;
    cout << "The current numbers are:"<<endl;
    HeapSort(A,100);
    for(int i=0;i<100;i++)cout<<A[i]<<"\t";
    cout << endl;
    cout << "The elapsed time is: " << double(clock()-start_time)<<"s"<<endl;
    return 0;

}

  • 归并排序

算法理解:
(具体参考:http://blog.csdn.net/yuehailin/article/details/68961304
代码实现:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void Merge(int elem[],int tmp[],int low,int mid,int high){
    int i=low,j=mid+1;
    int m=mid,n=high;
    int k=low;
    while(i<=m&&j<=n){
        if(elem[i]<elem[j]){
            tmp[k++]=elem[i++];
        }
        else
            tmp[k++]=elem[j++];
    }
    
    while(i<=m)tmp[k++]=elem[i++];
    while(j<=n)tmp[k++]=elem[j++];
    for(i=low;i<=high;i++){
        elem[i]=tmp[i];
    }
}
//
void Help(int elem[],int tmp[],int low,int high){
    if(low<high){
        int mid=(low+high)/2;
        Help(elem, tmp, low, mid);
        Help(elem,tmp,mid+1,high);
        Merge(elem, tmp, low, mid, high);
    }
}
//
void MergeSort(int elem[],int n){
    int *p=new int[n];
    Help(elem,p,0,n-1);
    delete[] p;
}
int main() {
    // insert code here...
    clock_t start_time=clock();
    srand(unsigned(time(NULL)));
    int A[100];
    const int min=1;
    const int max=100;
    cout<<"The previous numbers are:"<<endl;
    for(int i=0;i<100;i++)A[i]=rand()%(max-min+1)+min;
    for(int i=0;i<100;i++)cout<<A[i]<<"\t";
    cout<<endl;
    cout<<"The current numbers are:"<<endl;
    MergeSort(A,100);
    for(int i=0;i<100;i++)cout<<A[i]<<"\t";
    cout<<endl;
    cout<<"The elapsed time is: "<<double(clock()-start_time)<<"s"<<endl;
    cout<<endl;
    return 0;
}

以上均经过调试正确,代码仅供参考。


第一次修订时间:2017.12.26
第二次修订时间:2017.12.31(添加归并排序,内容并作部分修改)

    原文作者:排序算法
    原文地址: https://blog.csdn.net/richard__ting/article/details/78907193
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞