各种排序算法汇总
#include<iostream>
 #include<cstdio>
#include<cstdio>
 #include<algorithm>
#include<algorithm>
 #include<cmath>
#include<cmath>
 using namespace std;
using namespace std;

 template<class T>
template<class T>
 void InsertSort(T a[],int n)//直接插入排序,时间复杂度为O(n^2)
void InsertSort(T a[],int n)//直接插入排序,时间复杂度为O(n^2)
 {
{

 int i,j;
    int i,j;
 T temp;
    T temp;
 for(i=1;i<n;i++)
    for(i=1;i<n;i++)
 {
    {

 temp=a[i];
        temp=a[i];
 for(j=i-1;j>=0;j--)
        for(j=i-1;j>=0;j--)
 {
        {

 if(temp<a[j])
            if(temp<a[j])
 a[j+1]=a[j];
                a[j+1]=a[j];
 else
            else
 break;
                break;
 }
        }
 a[j+1]=temp;
        a[j+1]=temp;
 
        
 }
    }
 }
}



 template<class T>
template<class T>
 void ShellSort(T a[],int n)//希尔排序,时间复杂度为O(n^1.5)
void ShellSort(T a[],int n)//希尔排序,时间复杂度为O(n^1.5)

 //ShellSort
//ShellSort



 template<class T>
template<class T>
 void BubbleSort(T a[],int n)//冒泡排序,时间复杂度为O(n^2)
void BubbleSort(T a[],int n)//冒泡排序,时间复杂度为O(n^2)




 /**/
/**/
 template<class T>
template<class T>
 int Partion(T a[],int i,int j)//划分函数
int Partion(T a[],int i,int j)//划分函数




 template <class T>
template <class T>
 void qsort(T a[],int l,int h)
void qsort(T a[],int l,int h)
 {
{
 int m;
    int m;
 if(l<h)
    if(l<h) 
 {
    { 
 m=Partion(a,l,h);
        m=Partion(a,l,h);
 qsort(a,l,m-1);
        qsort(a,l,m-1);
 qsort(a,m+1,h);
        qsort(a,m+1,h); 
 }
    }
 }
}

 template<class T>
template<class T>
 void QuickSort(T a[],int n)
void QuickSort(T a[],int n)
 {
{
 qsort(a,0,n-1);
    qsort(a,0,n-1);
 }
}

 ////////////////////QuickSort_O(nlog2n)////////////////////////
////////////////////QuickSort_O(nlog2n)////////////////////////



 template <class T>
template <class T>
 void SelectSort(T a[],int n)//选择排序,时间复杂度为O(n^2)
void SelectSort(T a[],int n)//选择排序,时间复杂度为O(n^2)
 {
{
 int i,j,k;
    int i,j,k;
 T temp;
    T temp;
 for(i=0;i<n-1;i++)
    for(i=0;i<n-1;i++)
 {
    {   
 k=i;
        k=i;
 for(j=i+1;j<n;j++)
        for(j=i+1;j<n;j++)
 {
        {
 if(a[j]<a[k])
            if(a[j]<a[k])
 k=j;
                k=j;
 }
        }
 temp=a[k];
            temp=a[k];
 a[k]=a[i];
            a[k]=a[i];
 a[i]=temp;
            a[i]=temp;
 }
    }
 }
}

 ///////////////////////堆排序////////////////////////////
///////////////////////堆排序////////////////////////////
 template <class T>
template <class T>
 void Sift(T a[],int k,int m)//k筛选标号,m筛选范围
void Sift(T a[],int k,int m)//k筛选标号,m筛选范围
 {
{
 int i,j;
    int i,j;
 T temp;
    T temp;
 i=k;
    i=k;
 j=2*i+1;//j指向左儿子;
    j=2*i+1;//j指向左儿子;
 temp=a[i];//暂存a[i];
    temp=a[i];//暂存a[i];
 while(j<=m)
    while(j<=m)
 {
    { 
 if(j<m &&a[j]<a[j+1])
        if(j<m &&a[j]<a[j+1])
 j++;
                j++;
 if(temp<a[j])
        if(temp<a[j])
 {
        { 
 a[i]=a[j];
            a[i]=a[j];
 i=j;
            i=j;
 (j<<=1)++;//j*2+1
            (j<<=1)++;//j*2+1
 }
        }
 else
        else 
 break;
            break;
 }
    }
 a[i]=temp;
    a[i]=temp; 
 }
}        


 template <class T>
template <class T>
 void HeapSort(T a[],int n)//堆排序,建堆时间复杂度O(n),筛选O(nlog2n);
void HeapSort(T a[],int n)//堆排序,建堆时间复杂度O(n),筛选O(nlog2n);

 {
{
 int i;
    int i;
 T temp;
    T temp;
 for(i=(n>>1)-1;i>=0;i--)
    for(i=(n>>1)-1;i>=0;i--)
 Sift(a,i,n-1);
        Sift(a,i,n-1);

 for(i=n-1;i>=1;i--)
    for(i=n-1;i>=1;i--)
 {
    {  
 temp=a[0];
        temp=a[0];
 a[0]=a[i];
        a[0]=a[i];
 a[i]=temp;
        a[i]=temp;
 Sift(a,0,i-1);
        Sift(a,0,i-1);
 }
    }
 }
}
 /////////////////////HeapSort_O(nlog2n)///////////////////////////
/////////////////////HeapSort_O(nlog2n)///////////////////////////




 ///////////////////////归并排序,时间复杂度O(nlog2n)/////////////////////////////
///////////////////////归并排序,时间复杂度O(nlog2n)/////////////////////////////

 template <class T>
template <class T>
 void Merge(T sr[],T tr[],int l,int m,int n)
void Merge(T sr[],T tr[],int l,int m,int n)
 {
{
 int i,j,k;
    int i,j,k;
 i=l;
    i=l;
 j=m+1;
    j=m+1;
 k=l-1;
    k=l-1;
 while(i<=m && j<=n)
    while(i<=m && j<=n)
 {
    {
 if(sr[i]<sr[j])
        if(sr[i]<sr[j]) 
 tr[++k]=sr[i++];
            tr[++k]=sr[i++];
 else
        else 
 tr[++k]=sr[j++];
            tr[++k]=sr[j++];
 }
    }
 while(i<=m)
        while(i<=m)
 tr[++k]=sr[i++];
            tr[++k]=sr[i++];
 while(j<=n)
        while(j<=n)
 tr[++k]=sr[j++];
            tr[++k]=sr[j++];
 for(i=l;i<=n;i++)
        for(i=l;i<=n;i++) 
 sr[i]=tr[i];
            sr[i]=tr[i];
 }
}

 template <class T>
template <class T>
 void Msort(T a[],T st[],int s,int t)
void Msort(T a[],T st[],int s,int t)
 {
{
 int m;
    int m;
 if(s<t)
    if(s<t) 
 {
    { 
 m=(s+t)>>1;
        m=(s+t)>>1;
 Msort(a,st,s,m);
        Msort(a,st,s,m);
 Msort(a,st,m+1,t);
        Msort(a,st,m+1,t);
 Merge(a,st,s,m,t);
        Merge(a,st,s,m,t);
 }
    }
 }
}

 template <class T>
template <class T>
 void MergeSort(T a[],int n)
void MergeSort(T a[],int n)
 {
{ 
 T *st=new T[n];
    T *st=new T[n];
 Msort(a,st,0,n-1);
    Msort(a,st,0,n-1);  
 delete  [ ]st;
    delete  [ ]st;
 }
}
 //////////////////////MergeSort_O(nlog2n)///////////////////////////////
//////////////////////MergeSort_O(nlog2n)///////////////////////////////


 /////////////////////////END_TEMPLATE_BY_ABILITYTAO_ACM//////////////////////////////////////
/////////////////////////END_TEMPLATE_BY_ABILITYTAO_ACM//////////////////////////////////////





 int main ()
int main ()
 {
{
 double test1[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
    double test1[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
 InsertSort(test1,sizeof(test1)/sizeof(double));
    InsertSort(test1,sizeof(test1)/sizeof(double));
 double test2[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
    double test2[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
 ShellSort(test2,sizeof(test2)/sizeof(double));
    ShellSort(test2,sizeof(test2)/sizeof(double));
 double test3[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
    double test3[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
 BubbleSort(test3,sizeof(test3)/sizeof(double));
    BubbleSort(test3,sizeof(test3)/sizeof(double));
 double test4[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
    double test4[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
 QuickSort(test4,sizeof(test4)/sizeof(double));
    QuickSort(test4,sizeof(test4)/sizeof(double));
 double test5[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
    double test5[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
 SelectSort(test5,sizeof(test5)/sizeof(double));
    SelectSort(test5,sizeof(test5)/sizeof(double));
 double test6[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
    double test6[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
 HeapSort(test6,sizeof(test6)/sizeof(double));
    HeapSort(test6,sizeof(test6)/sizeof(double));
 double test7[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
    double test7[]={10.4,9.1,8.4,7,6,5,4,3,2,1};
 MergeSort(test7,sizeof(test7)/sizeof(double));
    MergeSort(test7,sizeof(test7)/sizeof(double));
 return 0;
    return 0;
 }
}







写这些函数的过程中我遇到了一个奇怪的问题,就是merge这个函数前不能使用上模板类,如果将它改为:
template <class T>
void merge(T sr[],T tr[],int l,int m,int n)
编译器竟然会报错,不知道是怎么回事,希望各位大牛指点一下。
呵呵 问题已经解决,原来在C++标准库里面已经使用过merge这个关键字,只要将merge改为Merge便可通过编译了。
来源:http://www.cppblog.com/abilitytao/archive/2009/05/06/82075.html
 
                     
                    
                 
                    
                
 
 
     
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号