排序数据结构(用模板实现)

排序数据结构(用模板实现)
 #include <iostream>
using namespace std;
#include <conio.h>
#include <algorithm>
//////////////////////////////////////////////////////////////////////////
//                 the bubble sort1
//////////////////////////////////////////////////////////////////////////
//template<class T>void sort(T* a,int n)
//{
//    for (int i=1;i<n;i++)
//        for(int j=1;j<=n-i;j++)
//            if(a[j-1]>=a[j])
//                swap(a[j-1],a[j]);
//
//}

//////////////////////////////////////////////////////////////////////////
//               the selection sort2
//////////////////////////////////////////////////////////////////////////
//template<class T>void sort(T* a,int n)
//{
//    for (int i=0;i<n-1;i++)
//    {
//        int min=i;
//        for(int j=i+1;j<n;j++)
//            if(a[j]<a[min])
//                min=j;
//                swap(a[min],a[i]);
//    }
//}
//////////////////////////////////////////////////////////////////////////
//              the insertion sort3
//////////////////////////////////////////////////////////////////////////
//template<class T>void sort(T* a,int n)
//{
//    for (int i=1;i<n;i++)
//    {
//        T temp,j;
//        temp=a[i];
//        for(j=i;j>0&&a[j-1]>temp;j--)
//        {
//            a[j]=a[j-1];
//        }
//            a[j]=temp;       
//    }   
//}

//////////////////////////////////////////////////////////////////////////
//             the Merge sort4
//////////////////////////////////////////////////////////////////////////
//template<class T>void merge(T* a,int n1,int n2)
//{
//    T* temp=new T[n1+n2];
//    int i=0,j1=0,j2=0;
//    while(j1<n1&&j2<n2)
//        temp[i++]=(a[j1]<=a[n1+j2]?a[j1++]:a[n1+j2++]);
//    while(j1<n1)
//        temp[i++]=a[j1++];
//    while(j2<n2)
//        temp[i++]=(a+n1)[j2++];
//    for(i=0;i<n1+n2;i++)
//        a[i]=temp[i];
//    delete []temp;
//}
//template<class T>void sort(T* a,int n)
//{
//    if (n>1)
//    {
//        int n1=n/2;
//        int n2=n-n1;
//        sort(a,n1);
//        sort(a+n1,n2);
//        merge(a,n1,n2);
//    }
//
//}
//////////////////////////////////////////////////////////////////////////
//              the quicksort sort5
//////////////////////////////////////////////////////////////////////////
//template<class T>void quicksort(T* a,int low,int high)
//{
//    if(low>=high)return;
//    T pivot=a[high];
//    int i=low-1;
//    int j=high;
//    while(i<j)
//    {
//        while(a[++i]<pivot);
//        while(j>=0&&a[--j]>pivot);
//        if(i<j)swap(a[i],a[j]);
//    }
//    swap(a[i],a[high]);
//    quicksort(a,low,i-1);
//    quicksort(a,i+1,high);
//   
//}
//template<class T>void sort(T* a,int n)
//{
//    quicksort(a,0,n-1);
//}

//////////////////////////////////////////////////////////////////////////
//              the heapsort sort6
//////////////////////////////////////////////////////////////////////////
//template<class T>void heapsort(T* a,int k,int n)
//{
//    T t=a[k];
//    while (k<n/2)
//    {
//        int j=2*k+1;
//        if(j+1<n&&a[j]<a[j+1])
//            ++j;
//        if(t>a[j])break;
//        a[k]=a[j];
//        k=j;
//    }
//    a[k]=t;
//}
//template<class T>void sort(T* a,int n)
//{
//    for(int i=n/2-1;i>=0;i--)
//        heapsort(a,i,n);
//    for (int i=n-1;i>0;i--)
//    {
//        swap(a[0],a[i]);
//        heapsort(a,0,i);
//    }
//}
////////////////////////////////////////////////////////////////////////
 //           the shellsort sort7
////////////////////////////////////////////////////////////////////////
template<class T>void shellsort(T* a,int n)
{
    int d=1,j;
    while (d<n/9)
        d=3*d+1;
    while (d>0)
    {
        for (int i=d;i<n;i++)
        {
            T t=a[i];
            j=i;
            while (j>=d&&a[j-d]>t)
            {
                a[j]=a[j-d];
                j-=d;
            }
            a[j]=t;
        }
        d/=3;
    }

}
template<class T>void sort(T* a,int n)
{
    shellsort(a,n);
}
template<class T>
void print(T* a,int n)
{
    //cout<<a[0];
    for (int i=0;i<n;i++)
    {
        cout<<"  "<<a[i];       
    }
    cout<<endl;
}
int main()
{
    int a[]={77,44,99,66,33,55,88,22,44};
    print(a,9);
    sort(a,9);
    print(a,9);
    getch();
}

你可能感兴趣的:(排序数据结构(用模板实现))