static void Main(string[] args) { int[] randomArray = { 2, 1, 8, 7, 9, 12, 0, 3 }; MergeSort(randomArray, 0, randomArray.Length - 1); for (int i = 0; i < randomArray.Length; ++i) Console.WriteLine(randomArray[i]); } public static void MergeSort(int[] A, int p, int r) { if(p < r) { int q = (p + r) / 2; MergeSort(A, p, q); MergeSort(A, q + 1, r); Merge(A, p, q, r); } } public static void Merge(int[] A, int p, int q, int r) { int n1 = q - p + 1; int n2 = r - q; int[] Left = new int[n1 + 1]; int[] Right = new int[n2 + 1]; for(int i1 = 0; i1 < n1; ++i1) { Left[i1] = A[p + i1]; } for(int j1 = 0; j1 < n2; ++j1) { Right[j1] = A[q + j1 + 1]; } Left[n1] = int.MaxValue; Right[n2] = int.MaxValue; int i = 0; int j = 0; for(int k = p; k <= r; ++k) { if(Left[i] <= Right[j]) { A[k] = Left[i]; ++i; } else { A[k] = Right[j]; ++j; } } }