JAVA语法——归并排序

参考了别的发的代码

package 归并排序;

public class 归并排序 {
    public static int[] sort(int [] a, int low, int high)
    {
        int mid = (low+high)/2;
        
        if(low < high)
        {
            sort(a,low,mid);
            sort(a,mid+1,high);
            
            merge(a,low,mid,high);
        }
        
        return a;
    }
    
    public static void merge(int[] a, int low, int mid, int high)
    {
        int[] temp = new int[high-low+1];
        int i = low;
        int j = mid+1;
        int k = 0;
        
        //把较小的数先移到数组中
        while(i <= mid && j <= high)
        {
            if(a[i] < a[j])
                temp[k++] = a[i++];
            else
                temp[k++] = a[j++];
        }
        
        //把左边剩余的数移入数组
        while(i <= mid)
        {
            temp[k++] = a[i++];
        }
        
        //把右边剩余的数移入数组
        while(j <= high)
        {
            temp[k++] = a[j++];
        }
        
        //把新数组中的数覆盖nums数组
        for(int x = 0; x < temp.length; x++)
            a[x+low] = temp[x];
    }
}

猜你喜欢

转载自www.cnblogs.com/JAYPARK/p/10289301.html