合併(歸併)排序 MergeSort

2021-08-26 04:39:36 字數 694 閱讀 4164

//用來計數,測試執行迴圈次數.

int count;

private void button1_click(object sender, eventargs e)

count = 0;

int theb = thea.toarray();

mergesort(theb, 0, n-1);

}private void mergesort(int a,int is,int ie)

//分左右兩段排序,2分法

int ie1 = (is + ie) / 2;

int is2 = ie1 + 1;

mergesort(a, is, ie1);

mergesort(a, is2, ie);

//排完左右後進行歸併針對兩個排好序的段(is-ie1,is2-ie)進行整理

int i1 = is, j1 = is2;//歸併初始都指向各自段得最小索引.

//因為都存放在陣列的is->ie段中,而且從小到大,從左到右存放。

//最大整理次數為ie-is,但一般只要整理完其中一段後即可.

while(true)

}else

a[i1 + 1] = tmp;

i1++; j1++;

}//因為兩個段都是排序的,右邊段如果整理完畢,則左邊段不再需要整理

if (j1 > ie)}}

歸併排序 合併排序(merge sort)詳解

前提 1.待排序的子串行相對有序。2.不考慮大資料等特殊情況。includeusing namespace std void merge sort int a,int p,int r void merge int a,int p,int q,int r int b 20 int main int l...

歸併排序 Merge sort

merge the a s.m and a m 1.t to r s.t template void two merge typet a,typet r,int s,int m,int t while i m r k a i while j t r k a j merge the a 0.n 1 s...

歸併排序(merge sort)

歸併排序 歸併排序是一種遞迴排序演算法,無論陣列元素的原始順序如何,其效能恆定不變。將陣列一分為二,分別排序兩部分元素,再將有序的兩半陣列歸併為乙個有序陣列。歸併步驟比較陣列前一半的元素與陣列的後一半元素,並將較小元素移到臨時陣列,該過程繼續前進,直到其中一半再沒有元素為止。此後只需將其餘元素移到臨...