歸並排序的定義
歸並排序算法采用的是分治算法,即把兩個(或兩個以上)有序表合並成一個新的有序表,即把待排序的序列分成若干個子序列,每個子序列都是有序的,然後把有序子序列合並成整體有序序列,這個過程也稱為2-路歸並.注意:歸並排序的一種穩定排序,即相等元素的順序不會改變.
歸並排序的原理
常見的排序主要有兩種,一種是先把待排序的序列一次分割,使子序列的長度減小至1,然後在合並,另外一種是把待排序兩兩分組排序然後在合並,具體過程用圖來解釋:
(1) 先分割再合並
待排序序列(14,12,15,13,11,16)
(2) 分組合並
待排序序列(25,57,48,37,12,92,86)
(圖片顯示不了,無語,有空畫一個。)
歸並排序實現的示例代碼:
#include<stdio.h>
//將有二個有序子數組a[begin...mid]和a[mid+1...end]合並。
void MergeArray(int a[],int begin,int mid,int end,int temp[])
{
int i=begin,j=mid+1;
int m=mid,n=end;
int k=0;
while(i<=m && j<=n)
{
if(a[i]<=a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=m)
temp[k++]=a[i++];
while(j<=n)
temp[k++]=a[j++];
//把temp數組中的結果裝回a數組
for(i=0;i<k;i++)
a[begin+i]=temp[i];
}
void mergesort(int a[],int begin,int end,int temp[])
{
if(begin<end)
{
int mid = (begin+end)/2;
mergesort(a,begin,mid,temp); //左邊有序
mergesort(a,mid+1,end,temp); //右邊有序
MergeArray(a,begin,mid,end,temp); //將左右兩邊有序的數組合並
}
}
int main()
{
int num[10]={2,5,9,3,6,1,0,7,4,8};
int temp[10];
mergesort(num,0,9,temp);
for(int i=0;i<10;i++)
{
printf("%d",num[i]);
}
printf("\n");
}
#include<stdio.h>
//將有二個有序子數組a[begin...mid]和a[mid+1...end]合並。
void MergeArray(int a[],int begin,int mid,int end,int temp[])
{
int i=begin,j=mid+1;
int m=mid,n=end;
int k=0;
while(i<=m && j<=n)
{
if(a[i]<=a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=m)
temp[k++]=a[i++];
while(j<=n)
temp[k++]=a[j++];
//把temp數組中的結果裝回a數組
for(i=0;i<k;i++)
a[begin+i]=temp[i];
}
void mergesort(int a[],int begin,int end,int temp[])
{
if(begin<end)
{
int mid = (begin+end)/2;
mergesort(a,begin,mid,temp); //左邊有序
mergesort(a,mid+1,end,temp); //右邊有序
MergeArray(a,begin,mid,end,temp); //將左右兩邊有序的數組合並
}
}
int main()
{
int num[10]={2,5,9,3,6,1,0,7,4,8};
int temp[10];
mergesort(num,0,9,temp);
for(int i=0;i<10;i++)
{
printf("%d",num[i]);
}
printf("\n");
}
歸並排序的時間復雜度
歸並排序的最好、最壞和平均時間復雜度都是O(nlogn),而空間復雜度是O(n),比較次數介於(nlogn)/2和(nlogn)-n+1,賦值操作的次數是(2nlogn)。因此可以看出,歸並排序算法比較占用內存,但卻是效率高且穩定的排序算法。