归并排序

归并排序(合并排序)
平均切分待排序区间,如果待排序区间的左右两个小区间已经有序,则按照合并有序数组的方式,使最终区间有序。

  1. 找中间位置,划分左右两个小区间,直到小区间长度 = =1或 < 1
  2. 分治思想,先排序左右两个小区间
  3. 合并有序数组
//归并排序
//[ , )
public static void mergeSort(int[] array){
    mergeSortInternal(array,0,array.length);
}
public static void mergeSortInternal(int[] array,int low,int high){
    if(low+1>=high){
        return ;
    }
    int mid=(low+high)/2;
    //[low,mid)
    //[mid,high)
    mergeSortInternal(array,low,mid);
    mergeSortInternal(array,mid,high);
    merge(array,low,mid,high);
}

合并两个有序数组
//[low,mid)- - 左区间
//[mid,high)- - 右区间

  1. 左区间,右区间同时遍历比较,小的数放到新的数组
  2. 左区间无数据,右区间未遍历完,把右区间的数直接补到新数组后
    左区间有数据,右区间无数据,亦如此
//合并有序数组
private static void merge(int[] array,int low,int mid,int high){
    int length=high-low;
    int[] extra=new int[length];
    //[low,mid)
    //[mid,high)
    int iLeft=low;
    int iRight=mid;
    int iExtra=0;
    while(iLeft<mid&&iRight<high){
        if(array[iLeft]<=array[iRight]){
            extra[iExtra++]=array[iLeft++];
        }else{
            extra[iExtra++]=array[iRight++];
        }
    }
    while(iLeft<mid){
        extra[iExtra++]=array[iLeft++];
    }
    while(iRight<high){
        extra[iExtra++]=array[iRight++];
    }
    for(int i=0;i<length;i++){
        array[low+i]=extra[i];
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值