import java.util.Arrays;
public class MyMergeSort {
public static void mergeSort(int[] nums, int low,int high){
if(low < high){
int mid = low + (high - low) / 2;
mergeSort(nums, low, mid);
mergeSort(nums, mid + 1, high);
merge(nums, low, mid, high);
}
}
public static void merge(int[] nums, int low, int mid, int high){
int left = low;
int right = mid + 1;
int[] arr = new int[nums.length];
int index = low;
while (left <= mid && right <= high){
if(nums[left] <= nums[right]){
arr[index++] = nums[left++];
}else{
arr[index++] = nums[right++];
}
}
while (left <= mid){
arr[index++] = nums[left++];
}
while (right <= high){
arr[index++] = nums[right++];
}
for(int i = low; i <= high; i++){
nums[i] = arr[i];
}
}
public static void main(String[] args){
int[] arr = {2, 4, 1, 9, 3, 0, -1};
mergeSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
}