
/**//*<排序算法模板匯總>
這些模板適用于任意數據類型,包括結構體和類類型;
如果是結構體或者是類類型,請重載"<"符號;
PS:這里均按照從小到大的順序來排序
Copyright:abilitytao,Nanjing University Of Science And Technology */






/**//////////////////////////BEGIN_TEMPLATE_BY_ABILITYTAO_ACM//////////////////////////////////////
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;

template<class T>
void InsertSort(T a[],int n)//直接插入排序,時間復雜度為O(n^2)


{

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

{

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

{

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



template<class T>
void ShellSort(T a[],int n)//希爾排序,時間復雜度為O(n^1.5)


{
int i,j,d;
T temp;
d=n>>1;
while(d>=1)

{
for(i=d;i<n;i++)

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

{
if(a[j]>temp)
a[j+d]=a[j];
else
break;
}
a[j+d]=temp;
}//這個while實際上是直接插入排序

d>>=1;//即d右移一位,d除以2;
}
}//ShellSort



template<class T>
void BubbleSort(T a[],int n)//冒泡排序,時間復雜度為O(n^2)


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

{
for(j=0;j<i;j++)

{
if(a[j]>a[j+1])

{

temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}



/**//////////////////////快速排序,時間復雜度為O(nlog2n)///////////////////////
template<class T>
int Partion(T a[],int i,int j)//劃分函數


{
T temp;
temp=a[i];
while(i<j)

{
while(i<j && temp<=a[j])
j--;
if(i<j)

{
a[i]=a[j];
i++;
}
while(i<j && a[i]<=temp)
i++;
if(i<j)

{
a[j]=a[i];
j--;
}
}
a[i]=temp;
return i;
}


template <class T>
void qsort(T a[],int l,int h)


{
int m;
if(l<h)

{
m=Partion(a,l,h);
qsort(a,l,m-1);
qsort(a,m+1,h);
}
}

template<class T>
void QuickSort(T a[],int n)


{
qsort(a,0,n-1);
}


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


template <class T>
void SelectSort(T a[],int n)//選擇排序,時間復雜度為O(n^2)


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

{
k=i;
for(j=i+1;j<n;j++)

{
if(a[j]<a[k])
k=j;
}
temp=a[k];
a[k]=a[i];
a[i]=temp;
}
}


/**////////////////////////堆排序////////////////////////////
template <class T>
void Sift(T a[],int k,int m)//k篩選標號,m篩選范圍


{
int i,j;
T temp;
i=k;
j=2*i+1;//j指向左兒子;
temp=a[i];//暫存a[i];
while(j<=m)

{
if(j<m &&a[j]<a[j+1])
j++;
if(temp<a[j])

{
a[i]=a[j];
i=j;
(j<<=1)++;//j*2+1
}
else
break;
}
a[i]=temp;
}


template <class T>
void HeapSort(T a[],int n)//堆排序,建堆時間復雜度O(n),篩選O(nlog2n);



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

for(i=n-1;i>=1;i--)

{
temp=a[0];
a[0]=a[i];
a[i]=temp;
Sift(a,0,i-1);
}
}

/**//////////////////////HeapSort_O(nlog2n)///////////////////////////




///////////////////////歸并排序,時間復雜度O(nlog2n)/////////////////////////////
template <class T>
void Merge(T sr[],T tr[],int l,int m,int n)


{
int i,j,k;
i=l;
j=m+1;
k=l-1;
while(i<=m && j<=n)

{
if(sr[i]<sr[j])
tr[++k]=sr[i++];
else
tr[++k]=sr[j++];
}
while(i<=m)
tr[++k]=sr[i++];
while(j<=n)
tr[++k]=sr[j++];
for(i=l;i<=n;i++)
sr[i]=tr[i];
}

template <class T>
void Msort(T a[],T st[],int s,int t)


{
int m;
if(s<t)

{
m=(s+t)>>1;
Msort(a,st,s,m);
Msort(a,st,m+1,t);
Merge(a,st,s,m,t);
}
}

template <class T>
void MergeSort(T a[],int n)


{
T *st=new T[n];
Msort(a,st,0,n-1);
delete [ ]st;
}

/**///////////////////////MergeSort_O(nlog2n)///////////////////////////////


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




int main ()


{

double test1[]=
{10.4,9.1,8.4,7,6,5,4,3,2,1};
InsertSort(test1,sizeof(test1)/sizeof(double));

double test2[]=
{10.4,9.1,8.4,7,6,5,4,3,2,1};
ShellSort(test2,sizeof(test2)/sizeof(double));

double test3[]=
{10.4,9.1,8.4,7,6,5,4,3,2,1};
BubbleSort(test3,sizeof(test3)/sizeof(double));

double test4[]=
{10.4,9.1,8.4,7,6,5,4,3,2,1};
QuickSort(test4,sizeof(test4)/sizeof(double));

double test5[]=
{10.4,9.1,8.4,7,6,5,4,3,2,1};
SelectSort(test5,sizeof(test5)/sizeof(double));

double test6[]=
{10.4,9.1,8.4,7,6,5,4,3,2,1};
HeapSort(test6,sizeof(test6)/sizeof(double));

double test7[]=
{10.4,9.1,8.4,7,6,5,4,3,2,1};
MergeSort(test7,sizeof(test7)/sizeof(double));
return 0;
}







寫這些函數的過程中我遇到了一個奇怪的問題,就是merge這個函數前不能使用上模板類,如果將它改為:
template <class T>
void merge(T sr[],T tr[],int l,int m,int n)
編譯器竟然會報錯,不知道是怎么回事,希望各位大牛指點一下。
呵呵 問題已經解決,原來在C++標準庫里面已經使用過merge這個關鍵字,只要將merge改為Merge便可通過編譯了。
謹以此文,紀念數據結構最后一課。