public class MergeSort { private static void sort(int[] a, int i, int length, int[] b) { if (i < length) { int mid = (length + i) / 2; sort(a, i, mid, b); sort(a, mid + 1, length, b); merge(a, b, mid, i, length); } } private static void merge(int[] a, int[] b, int mid, int i, int length) { int t = 0; int o = i; int k = mid + 1; while (i <= mid && k <= length) { if (a[i] > a[k]) { b[t++] = a[k++]; } else { b[t++] = a[i++]; } } while (i <= mid) { b[t++] = a[i++]; } while (k <= length) { b[t++] = a[k++]; } t = 0; while (o <= length) { a[o++] = b[t++]; } } public static void main(String[] args) { int a[] = { 3, 2, 1, 6, 8 }; int b[] = new int[a.length]; sort(a, 0, a.length - 1, b); System.out.println(Arrays.toString(a)); } }