多种排序功能的实现

#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<ctime>
#include<cstdio>
#include<iomanip>
#include<conio.h>
using namespace std;
template<class T>
void InsertSort(T* pa,int n)
{
    T temp;
    for(int i=1; i<n; i++)
    {
        temp=pa[i];
        int j=i;
        while(j>=1&&temp<pa[j-1])
        {
            pa[j]=pa[j-1];
            j--;
        }
        pa[j]=temp;
    }
}
template<class T>
void ShellSort(T* pa,int n)
{
    T temp;
    int gap=n/2;
    while(gap)
    {
        for(int start=gap; start<2*gap&&start<n; start++)
        {
            for(int i=start; i<n; i+=gap)
            {
                temp=pa[i];
                int j=i;
                while(j>=gap&&temp<pa[j-gap])
                {
                    pa[j]=pa[j-gap];
                    j-=gap;
                }
                pa[j]=temp;
            }
        }
        gap=gap/2;
    }
}
template<class T>
void BubbleSort(T* pa,int n)
{
    T temp;
    int i=0;
    while(i<n-1)
    {
        int last=n-1;
        for(int j=n-1; j>i; j--)
        {
            if(pa[j]<pa[j-1])
            {
                temp=pa[j-1];
                pa[j-1]=pa[j];
                pa[j]=temp;
                last=j;
            }
        }
        i=last;
    }
}
template<class T>
void SelectSort(T* pa,int n)
{
    T temp;
    for(int i=0; i<n-1; i++)
    {
        int min=i;
        for(int j=i+1; j<n; j++)
        {
            if(pa[j]<pa[min])
                min=j;
        }
        if(min!=i)
        {
            temp=pa[i];
            pa[i]=pa[min];
            pa[min]=temp;
        }
    }
}
template<class T>
int Partition(T* pa,int low,int high)
{
    int i=low,j=high;
    T temp=pa[i];
    while(i!=j)
    {
        while(pa[j]>=temp&&j>i)
        {
            j--;
        }
        if(j>i)
        {
            pa[i++]=pa[j];
        }
        while(pa[i]<=temp&&i<j)
        {
            i++;
        }
        if(i<j)
        {
            pa[j--]=pa[i];
        }
    }
    pa[i]=temp;
    return i;
}
template <class T>
void QuickSort(T* pa,int low,int high)
{
    if(low>=high)
    {
        return ;
    }
    int m=Partition(pa,low,high);
    QuickSort(pa,low,m-1);
    QuickSort(pa,m+1,high);
}
template<class T>
void QuickSort(T* pa,int n)
{
    QuickSort(pa,0,n-1);
}
template <class T>
void display(T* pa,int n)
{
    for(int i=0; i<n; i++)
    {
        cout<<pa[i]<<' ';
    }
    cout<<endl;
}

template<class T>
void init(T * pa,int n)
{
    for(int i=0; i<n; i++)
    {
        pa[i]=rand()%20000;
    }
    cout<<"原始数组(随机生成):"<<endl;
    display(pa,n);
}
template<class T>
void Merge(T* ini,T* merge,int s,int m,int e)
{
 
}
int main()
{
    ios::sync_with_stdio(false);
    int n,m;
    int a[20005];
    long double be,la;
    cout<<"输入排序数组的长度:"<<endl;
    while(cin>>n)
    {
        cout<<"输入你想进行什么排序:"<<endl;
        cout<<"1.插入排序\t 2.希尔排序\t 3.起泡排序\t 4.快速排序\t 5.sort"<<endl;
        cin>>m;
        system("cls");
        init(a,n);
        if(m==1)
        {
            cout<<"插入排序:"<<endl;
            be=clock();
            InsertSort(a,n);
            la=clock();
        }
        if(m==2)
        {
            cout<<"希尔排序:"<<endl;
            be=clock();
            ShellSort(a,n);
            la=clock();
        }
        if(m==3)
        {
            cout<<"起泡排序:"<<endl;
            be=clock();
            BubbleSort(a,n);
            la=clock();
        }
        if(m==4)
        {
            cout<<"快速排序:"<<endl;
            be=clock();
            QuickSort(a,n);
            la=clock();
        }
        if(m==5)
        {
            cout<<"sort23333:"<<endl;
            be=clock();
            sort(a,a+n);
            la=clock();
        }
        cout<<"排序之后:"<<endl;
        display(a,n);
        cout<<"用时:"<<fixed<<setprecision(12)<<(la-be)/CLOCKS_PER_SEC<<'s'<<endl;
        getch();
        system("cls");
        cout<<"输入排序数组的长度:";
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Numblzw/p/10022795.html