1. 排序算法

2. 归并排序

class Solution{
    public static void main(String[] args) {
        int[] nums1 = {1,3,2,5,9,0};
        merge(nums1,0,nums1.length-1);
        System.out.println(Arrays.toString(nums1));

    }

    public static void merge(int[] nums,int low,int high){
        int middle = low + (high-low)/2;
        if(low < high){
            merge(nums,low,middle);
            merge(nums,middle+1,high);
            mergeSort(nums,low,middle,high);
        }
    }

    public static void mergeSort(int[] nums,int low ,int middle,int high){
        int i= low;
        int j = middle+1;
        int index = 0;
        int[] tempArray = new int[nums.length];
        while(i<=middle && j<=high){
            if(nums[i] < nums[j]){
                tempArray[index] = nums[i];
                index++;
                i++;
            }
            else{
                tempArray[index] = nums[j];
                index++;
                j++;
            }
        }

        while(i<=middle){
            tempArray[index] = nums[i];
            index++;
            i++;
        }
        while(j<=high){
            tempArray[index] = nums[j];
            index++;
            j++;
        }
        for(int k=0;k<index;k++){
            nums[k+low] = tempArray[k];
        }
    }   
}
© gaohueric all right reserved,powered by Gitbook文件修订时间: 2021-12-08 23:22:22

results matching ""

    No results matching ""