Java-归并排序

来源:互联网 发布:网络发帖推广 编辑:程序博客网 时间:2024/05/29 13:56
public void mergeSort(int[] a, int first, int last, int[] temp){
        if(first<last){
            int mid = (first+last)/2;
            mergeSort(a,first,mid,temp);       //左边有序
            mergeSort(a,mid+1,last,temp);   //右边有序
            
            //把相邻两个有序的数组合并,形成一个新的有序数组
            mergeArray(a,first,mid,last,temp);
        }
    }

    private void mergeArray(int[] a, int first, int mid, int last, int[] temp) {
        int i=first,m=mid;
        int j=mid+1,n=last;
        
        int count = 0;//记录这次合并了几个数
        
        while(i<=m && j<=n){
            if(a[i] <=a[j]){
                temp[count++] = a[i++];
            } else{
                temp[count++] = a[j++];
            }
        }
        
        while(i<=m){
            temp[count++] = a[i++];
        }
        
        while(j<=n){
            temp[count++] = a[j++];
        }
        
        //把临时数组排好序的部分拷贝回原始数组
        for(int k=0; k<count; k++){
            a[first+k] = temp[k];
        }
    }
0 0