归并排序原理归并 排序具体工作原理如下(假设序列共有n个元素):将序列每相邻两个数字进行归并操作(merge),形成floor(n/2)个序列,排序后每个序列包含两个元素将上述序列再次归并,形成floor(n/4)个序列,每个序列包含四个元素重复步骤2,直到所有元素排序完毕示例代码Go语言#include const int MAXN=200005; int n, a[MAXN], temp[MAXN]; long long ans; void count(int l, int r) { if(r == l) return ;//结束条件 int m = (l + r) >> 1; count(l, m); count(m + 1, r);//二分查找 int i = l, j = m + 1, k = l; while(j <= r || i <= m) { if(j > r || (i <= m && a[i] < a[j])) temp[k++] = a[i++]; else temp[k++] = a[j++], ans += m - i + 1; } for(i = l; i <= r; i++) a[i] = temp[i]; } int main() { scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); count(1, n);//调用函数 printf("%lld", ans);//输出 return 0; }Java语言package MergeSort; public class MergeSort { public static int[] mergeSort(int[] nums, int l, int h) { if (l == h) return new int[] { nums[l] }; int mid = l + (h - l) / 2; int[] leftArr = mergeSort(nums, l, mid); //左有序数组 int[] rightArr = mergeSort(nums, mid + 1, h); //右有序数组 int[] newNum = new int[leftArr.length + rightArr.length]; //新有序数组 int m = 0, i = 0, j = 0; while (i < leftArr.length && j < rightArr.length) { newNum[m++] = leftArr[i] < rightArr[j] ? leftArr[i++] : rightArr[j++]; } while (i < leftArr.length) newNum[m++] = leftArr[i++]; while (j < rightArr.length) newNum[m++] = rightArr[j++]; return newNum; } public static void main(String[] args) { int[] nums = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 10 }; int[] newNums = mergeSort(nums, 0, nums.length - 1); for (int x : newNums) { System.out.println(x); } } }Python语言def MergeSort(lists): if len(lists) <= 1: return lists num = int( len(lists) / 2 ) left = MergeSort(lists[:num]) right = MergeSort(lists[num:]) return Merge(left, right) def Merge(left,right): r, l=0, 0 result=[] while l #include void Merge(int sourceArr[],int tempArr[], int startIndex, int midIndex, int endIndex) { int i = startIndex, j=midIndex+1, k = startIndex; while(i!=midIndex+1 && j!=endIndex+1) { if(sourceArr[i] > sourceArr[j]) tempArr[k++] = sourceArr[j++]; else tempArr[k++] = sourceArr[i++]; } while(i != midIndex+1) tempArr[k++] = sourceArr[i++]; while(j != endIndex+1) tempArr[k++] = sourceArr[j++]; for(i=startIndex; i<=endIndex; i++) sourceArr[i] = tempArr[i]; } //内部使用递归 void MergeSort(int sourceArr[], int tempArr[], int startIndex, int endIndex) { int midIndex; if(startIndex < endIndex) { midIndex = startIndex + (endIndex-startIndex) / 2;//避免溢出int MergeSort(sourceArr, tempArr, startIndex, midIndex); MergeSort(sourceArr, tempArr, midIndex+1, endIndex); Merge(sourceArr, tempArr, startIndex, midIndex, endIndex); } } int main(int argc, char * argv[]) { int a = {50, 10, 20, 30, 70, 40, 80, 60}; int i, b; MergeSort(a, b, 0, 7); for(i=0; i<8; i++) printf("%d ", a[i]); printf("\n"); return 0; }PHP语言//merge函数将指定的两个有序数组(arr1,arr2)合并并且排序 //我们可以找到第三个数组,然后依次从两个数组的开始取数据哪个数据小就先取哪个的,然后删除掉刚刚取过///的数据 function al_merge($arrA,$arrB) { $arrC = array(); while(count($arrA) && count($arrB)) { //这里不断的判断哪个值小,就将小的值给到arrC,但是到最后肯定要剩下几个值, //不是剩下arrA里面的就是剩下arrB里面的而且这几个有序的值,肯定比arrC里面所有的值都大所以使用 $arrC[] = $arrA['0'] < $arrB['0'] ? array_shift($arrA) : array_shift($arrB); } return array_merge($arrC, $arrA, $arrB); } //归并排序主程序 function al_merge_sort($arr) { $len = count($arr); if($len <= 1) return $arr;//递归结束条件,到达这步的时候,数组就只剩下一个元素了,也就是分离了数组 $mid = intval($len/2);//取数组中间 $left_arr = array_slice($arr, 0, $mid);//拆分数组0-mid这部分给左边left_arr $right_arr = array_slice($arr, $mid);//拆分数组mid-末尾这部分给右边right_arr $left_arr = al_merge_sort($left_arr);//左边拆分完后开始递归合并往上走 $right_arr = al_merge_sort($right_arr);//右边拆分完毕开始递归往上走 $arr = al_merge($left_arr, $right_arr);//合并两个数组,继续递归 return $arr; } $arr = array(12, 5, 4, 7, 8, 3, 4, 2, 6, 4, 9); print_r(al_merge_sort($arr));Pascal语言program　mergesort_1; const maxn=7; type arr=array[1..maxn] of integer; var a,b,c:arr; i:integer; procedure merge(r:arr; l,m,n:integer; var r2:arr); var i,j,k,p:integer; begin i:=l; j:=m+1; k:=l-1; while (i<=m) and (j<=n) do begin k:=k+1; if r[i]<=r[j] then begin r2[k]:=r[i]; i:=i+1 end else begin r2[k]:=r[j]; j:=j+1; end end; if i<=m then for p:=i to m do begin k:=k+1; r2[k]:=r[p]; end; if j<=n then for p:=j to n do begin k:=k+1; r2[k]:=r[p]; end; end; procedure mergesort(var r,r1:arr; s,t:integer); var k:integer; c:arr; begin if s=t then r1[s]:=r[s] else begin k:=(s+t)div2; mergesort(r,c,s,k); mergesort(r,c,k+1,t); merge(c,s,k,t,r1) end; end; begin write('Enterdata:'); for i:=1 to maxn do read(a[i]); mergesort(a,b,1,maxn); for i:=1 to maxn do write(b[i]:9); writeln; end. //============================================ program mergesort_2; const max=100000; var a,r:array[1..max] of longint; n,i:longint; procedure msort(s,t:longint); var m,i,j,k:longint; begin if s=t then exit; m:=(s+t) div 2; msort(s,m); msort(m+1,t); i:=s; j:=m+1; k:=s; while (i<=m) and (j<=t) do begin if a[i]