实现如下:

#region Merge Sort
private static void Merge(int[] data, int startIndex1, int endIndex1, int startIndex2, int endIndex2)
{
if (data == null || data.Length < 1)
{
throw new ArgumentNullException("data");
}
if (startIndex1 < 0 || startIndex2 < 0 || endIndex1 < startIndex1 || endIndex2 < startIndex2 ||
endIndex1 > data.Length || endIndex2 > data.Length)
{
throw new ArgumentException("index");
}
int[] tempArray = new int[endIndex1 - startIndex1 + endIndex2 - startIndex2 + 2];
int index1 = startIndex1, index2 = startIndex2;
int tempIndex = 0;
while(index1 <= endIndex1 && index2 <= endIndex2)
{
if (data[index1] <= data[index2])
{
tempArray[tempIndex++] = data[index1++];
}
else
{
tempArray[tempIndex++] = data[index2++];
}
}
while (index1 <= endIndex1)
{
tempArray[tempIndex++] = data[index1++];
}
while (index2 <= endIndex2)
{
tempArray[tempIndex++] = data[index2++];
}
tempIndex = 0;
for (int i = startIndex1; i <= endIndex1; i++)
{
data[i] = tempArray[tempIndex++];
}
for (int i = startIndex2; i <= endIndex2; i++)
{
data[i] = tempArray[tempIndex++];
}
}
private static void MergePass(int[] data, int cellLength)
{
if (data == null || data.Length < 1)
{
throw new ArgumentNullException("data");
}
if (cellLength < 1 || cellLength > data.Length)
{
throw new ArgumentException("cellLength");
}
int index = 0;
while (index <= data.Length - cellLength * 2)
{
Merge(data, index, index + cellLength - 1, index + cellLength, index + cellLength * 2 - 1);
index += 2 * cellLength;
}
if (index + cellLength < data.Length)
{
Merge(data, index, index + cellLength - 1, index + cellLength, data.Length - 1);
}
}
/// <summary>
/// 二路归并排序
/// </summary>
/// <param name="data"></param>
public static void MergeSort(int[] data)
{
if (data == null || data.Length < 1)
{
throw new ArgumentNullException("data");
}
int cellLength = 1;
while (cellLength < data.Length)
{
MergePass(data, cellLength);
cellLength *= 2;
}
}
#endregion
private static void Merge(int[] data, int startIndex1, int endIndex1, int startIndex2, int endIndex2)
{
if (data == null || data.Length < 1)
{
throw new ArgumentNullException("data");
}
if (startIndex1 < 0 || startIndex2 < 0 || endIndex1 < startIndex1 || endIndex2 < startIndex2 ||
endIndex1 > data.Length || endIndex2 > data.Length)
{
throw new ArgumentException("index");
}
int[] tempArray = new int[endIndex1 - startIndex1 + endIndex2 - startIndex2 + 2];
int index1 = startIndex1, index2 = startIndex2;
int tempIndex = 0;
while(index1 <= endIndex1 && index2 <= endIndex2)
{
if (data[index1] <= data[index2])
{
tempArray[tempIndex++] = data[index1++];
}
else
{
tempArray[tempIndex++] = data[index2++];
}
}
while (index1 <= endIndex1)
{
tempArray[tempIndex++] = data[index1++];
}
while (index2 <= endIndex2)
{
tempArray[tempIndex++] = data[index2++];
}
tempIndex = 0;
for (int i = startIndex1; i <= endIndex1; i++)
{
data[i] = tempArray[tempIndex++];
}
for (int i = startIndex2; i <= endIndex2; i++)
{
data[i] = tempArray[tempIndex++];
}
}
private static void MergePass(int[] data, int cellLength)
{
if (data == null || data.Length < 1)
{
throw new ArgumentNullException("data");
}
if (cellLength < 1 || cellLength > data.Length)
{
throw new ArgumentException("cellLength");
}
int index = 0;
while (index <= data.Length - cellLength * 2)
{
Merge(data, index, index + cellLength - 1, index + cellLength, index + cellLength * 2 - 1);
index += 2 * cellLength;
}
if (index + cellLength < data.Length)
{
Merge(data, index, index + cellLength - 1, index + cellLength, data.Length - 1);
}
}
/// <summary>
/// 二路归并排序
/// </summary>
/// <param name="data"></param>
public static void MergeSort(int[] data)
{
if (data == null || data.Length < 1)
{
throw new ArgumentNullException("data");
}
int cellLength = 1;
while (cellLength < data.Length)
{
MergePass(data, cellLength);
cellLength *= 2;
}
}
#endregion