编辑代码

class Main {
	public static void main(String[] args) {
        //归并排序
        int[] arr = {1, 3, 4, 8, 2, 5, 6, 7};
        merge(arr, 2, 4, 5 );
	}

    static void merge(int[] arr, int leftP, int rightP, int rightBound){ 
        int[] temp = new int[rightBound - leftP + 1];
        int k = 0; //temp指针 
        int i = leftP; //前指针
        int j = rightBound; //后指针

        while ( i < rightP && j < rightBound ){ 
            temp[k++] = arr[i] <= arr[j] ? arr[i++] : arr[j++];
        }
        while ( i < rightP ) temp[k++] = arr[i++];
        while ( j < rightBound ) temp[k++] = arr[j++];

        print(temp);
    }

    static void print(int[] arr){
        System.out.println("输出数组:\n");
        for ( int i = 0; i < arr.length; i++){
            System.out.print(arr[i] + " ");
        }
    }
}