voidStart() { MergeSort (8, S); for (int i = 0; i < S.Length; i++) { Debug.Log (S [i]); } } voidMergeSort(int n, int[] data) { if (n > 1) { int h = n / 2 , m= n - h; int[] left = newint[h]; int[] right = newint[m]; for (int x = 0; x < h; x++) { left [x] = data [x]; } for (int y = 0; y < m; y++) { right [y] = data [h + y]; } MergeSort (h, left); MergeSort (m, right); Merge (data, left, right); } }
voidMerge(int[] data, int[] left, int[] right) { int i = 0, j = 0,k = 0; while (j<left.Length&&k<right.Length) { if (left [j] < right [k]) { data [i] = left [j]; j += 1; } else { data [i] = right [k]; k += 1; } i += 1; } if (j == left.Length-1) while (k < left.Length) data [i++] = right [k++]; else while (j < right.Length) data [i++] = left [j++];