public class Merge_sort{
public static void main(String[] args) {
int[] num = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
sort(num, 0, num.length - 1);
for (int i = 0; i < num.length; i++) {
System.out.println(num[i]);
}
}
public static void sort(int[] num, int low, int high) {
int mid = low + (high - low) / 2;
if (low < high) {
sort(num, low, mid);
sort(num, mid + 1, high);
merge(num, low, mid, high);
}
}
public static void merge(int[] num, int low, int mid, int high) {
int[] temp = new int[high - low + 1];
int i = low, j = mid + 1;
int index = 0;
while (i <= mid && j <= high) {
if (num[i] <= num[j]) {
temp[index++] = num[i++];
} else {
temp[index++] = num[j++];
}
}
while (i <= mid) {
temp[index++] = num[i++];
}
while (j <= high) {
temp[index++] = num[j++];
}
for (int p = 0; p < temp.length; p++) {
num[low + p] = temp[p];
}
}
}