# C#算法设计排序篇之05-归并排序（附带动画演示程序）

C#算法设计概述

## 示例

```public class Program {

public static void Main(string[] args) {
int[] array = { 43, 69, 11, 72, 28, 21, 56, 80, 48, 94, 32, 8 };

MergeSort(array, 0, array.Length - 1);
ShowSord(array);

}

private static void ShowSord(int[] array) {
foreach (var num in array) {
Console.Write(\$"{num} ");
}
Console.WriteLine();
}

public static void MergeSort(int[] array, int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
MergeSort(array, low, mid);
MergeSort(array, mid + 1, high);
Merge(array, low, mid, high);
}
}

private static void Merge(int[] array, int low, int mid, int high) {
int[] mergeArr = new int[high - low + 1];
int left = low;
int right = mid + 1;
int merge = 0;
while (left <= mid && right <= high) {
if (array[left] <= array[right]) {
mergeArr[merge++] = array[left++];
}
else {
mergeArr[merge++] = array[right++];
}
}
while (left <= mid) {
mergeArr[merge++] = array[left++];
}
while (right <= high) {
mergeArr[merge++] = array[right++];
}
merge = 0;
while (low <= high) {
array[low++] = mergeArr[merge++];
}
}

}```

以上是归并排序算法的一种实现，以下是这个案例的输出结果：

`8 11 21 28 32 43 48 56 69 72 80 94`

## AlgorithmMan

AlgorithmMan by Iori，AlgorithmMan是使用C#开发的一套用于算法演示的工具。

(6)