歸並排序
歸並排序有兩種實現方法
(
自底向上的基本思想是
n為偶數

參與歸並)

後一個子文件長度仍為

序的子文件兩兩歸並
上述的每次歸並操作
(
【 參見動畫演示 】
(
分析
在某趟歸並中

子文件
[

注意
調用歸並操作將相鄰的一對子文件進行歸並時
情況進行特殊處理
① 若子文件個數為奇數
② 若子文件個數為偶數
具體算法如下
void MergePass(SeqList R
{ //對R[
int i;
for(i=
Merge(R
//歸並長度為length的兩個相鄰子文件
if(i+length Merge(R,i,i+length-1,n); //歸並最後兩個子文件 //注意:若i≤n且i+length-1≥n時,則剩余一個子文件輪空,無須歸並 } //MergePass (4)二路歸並排序算法 void MergeSort(SeqList R) {//采用自底向上的方法,對R[1..n]進行二路歸並排序 int length; for(1ength=1;length MergePass(R,length); //有序段長度≥n時終止 } 注意: 自底向上的歸並排序算法雖然效率較高,但可讀性較差。tW.WiNgWit.cOM
From:http://tw.wingwit.com/Article/program/sjjg/201311/23777.html