# 经典排序算法 - 归并排序Merge sort

--------------------

--------------------

--------------------

--------------------

--------------------

[ 1 2 4 6 ]与[ 5 9 ]的合并过程与上边一样,不再分解

        static void merge(int[] unsorted, int first, int mid, int last, int[] sorted)
{
int i = first, j = mid;
int k = 0;
while (i < mid && j < last)
if (unsorted[i] < unsorted[j])
sorted[k++] = unsorted[i++];
else
sorted[k++] = unsorted[j++];

while (i < mid)
sorted[k++] = unsorted[i++];
while (j < last)
sorted[k++] = unsorted[j++];

for (int v = 0; v < k; v++)
unsorted[first + v] = sorted[v];
}

static void merge_sort(int[] unsorted, int first, int last, int[] sorted)
{
if (first + 1 < last)
{
int mid = (first + last) / 2;
Console.WriteLine("{0}-{1}-{2}", first, mid, last);
merge_sort(unsorted, first, mid, sorted);
merge_sort(unsorted, mid, last, sorted);
merge(unsorted, first, mid, last, sorted);
}
}

static void Main(string[] args)
{
int[] x = { 6, 2, 4, 1, 5, 9 };
int[] sorted = new int[x.Length];
merge_sort(x, 0, x.Length, sorted);
for (int i = 0; i < sorted.Length; i++)
{
if (x[i] > 0)
Console.WriteLine(x[i]);
}
}