Data structure training 5-sorting algorithm application

  1. The purpose of the experiment:
    (1) To be proficient in the commonly used inner sorting methods and to compare them.
  2. Experiment content:
    Use random function to generate N random integers (above 20000), and sort these numbers in a variety of ways. Requirements:
    (1) At least three methods are used to solve the above problems (hint, the methods that can be used are Hill sorting, bubble sorting, quick sorting, selective sorting, and heap sorting). And save the sorted results in different files.
    (2) Count the performance of each sorting method (the time spent on running the program on the above computer is subject to comparison), and find out two of the faster methods.
    3. Experimental code:
#include<bits/stdc++.h>
using namespace std;
const int maxn=20000+100;
int a[maxn],n,b[maxn];
void sortloop(int n,int *b)
{
    
    
    for(int i=1; i<n; i++)
    {
    
    
        for(int j=i+1; j<=n; j++)
            if(b[i]>b[j]) swap(b[i],b[j]);
    }
}
void sortquick(int l,int r,int *b)
{
    
    
    if(l<r)
    {
    
    
        int val=b[l];
        int low=l,high=r;
        while(low<high)
        {
    
    
            while(b[high]>=val&&low<high) high--;
            b[low]=b[high];
            while(b[low]<=val&&low<high) low++;
            b[high]=b[low];
        }
        a[low]=val;
        sortquick(l,low-1,b);
        sortquick(low+1,r,b);
    }
}
void sortshell(int n,int *b){
    
    
    /*for(int gap=2*n;gap;gap=gap/2){
        for(int i=gap;i<=n;i++){
            for(int j=i-gap;j>=1;j-=gap){
                if(b[j]>b[j+gap]) swap(b[j],b[j+gap]);///不优化
            }
        }
    }*/
    for(int gap=2*n;gap;gap=gap/2){
    
    
        for(int i=1;i<=gap;i++){
    
    ///gap组
            for(int j=i+gap;j<=n;j+=gap){
    
    
                if(b[j]<b[j-gap]){
    
    
                    int tmp=b[j],q=j-gap;
                    while(q>=1&&b[q]>tmp){
    
    
                        b[q+gap]=b[q];q-=gap;
                    }
                    b[q+gap]=tmp;
                }
            }
        }
    }
}
int main()
{
    
    
    srand(time(0));//随机数种子
    clock_t s,e;
    n=(rand()*rand()+rand())%20000+1;///随机生成数组的长度
    for(int i=1; i<=n; i++)
    {
    
    
        a[i]=(rand()*rand()+rand())%10000+1;///随机生成数组内的元素
        b[i]=a[i];///拷贝
    }
    cout<<"原数组的元素为:"<<endl;
     /*for(int i=1;i<=n;i++){
         cout<<a[i]<<" ";
         if(i%5==0) puts("");
     }*/
    ///选择排序 希尔排序 堆排序
    s=clock();///计算代码运行时间
    cout<<"冒泡排序后的元素为"<<endl;
    sortloop(n,b);
    /*for(int i=1;i<=n;i++){
        cout<<b[i]<<" ";
        if(i%5==0) puts("");
    }*/
    e=clock();
    cout<<"冒泡排序花费的时间为:"<<(double(e-s)/CLOCKS_PER_SEC)<<"s\n";

    s=clock();///计算代码运行时间
    cout<<"快速排序后的元素为"<<endl;
    for(int i=1; i<=n; i++) b[i]=a[i];
    sortquick(1,n,b);
    /*for(int i=1;i<=n;i++){
        cout<<b[i]<<" ";
        if(i%5==0) puts("");
    }*/
    e=clock();
    cout<<"快速排序花费的时间为:"<<(double(e-s)/CLOCKS_PER_SEC)<<"s\n";

    s=clock();///计算代码运行时间
    cout<<"希尔排序后的元素为"<<endl;
    for(int i=1; i<=n; i++) b[i]=a[i];
    sortshell(n,b);
    /*for(int i=1;i<=n;i++){
        cout<<b[i]<<" ";
        if(i%5==0) puts("");
    }*/
    e=clock();
    cout<<"希尔排序花费的时间为:"<<(double(e-s)/CLOCKS_PER_SEC)<<"s\n";

    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45675097/article/details/111172722