zoukankan      html  css  js  c++  java
  • Merge Sort

    character:the sort time is proportional to any input array with length of N.

    Introduction to merge:if two arrays are already sorted,after merge operation,the combined array is also sorted.

    the basic realization of merge function:

    public static void merge(Comparable[] a,int lo,int hi){
    if(hi<=lo) return;
    boolean flag=true;

    int mid=lo+(hi-lo)/2;
    int head=lo;
    int tail=mid+1;
    int index=lo;

    for(int i=lo;i<=hi;++i){
    aux[i]=a[i];
    }
    while(flag){
    boolean inHead= (head<=mid);
    boolean inTail= (tail<=hi);

    if((head<=mid) && (tail<=hi)){
    if(less(aux[head],aux[tail])){
    a[index++]=aux[head++];
    }else{
    a[index++]=aux[tail++];
    }
    }

    if((inHead) && (!inTail)){
    while(head<=mid){
    a[index++]=aux[head++];
    }
    }

    if((!inHead) && (inTail)){
    while(tail<=hi){
    a[index++]=aux[tail++];
    }
    }

    if((!inHead) && (!inTail)){
    flag=false;
    }
    }
    }

    the update one:

    public static void updateMerge(Comparable[] a,int lo,int hi){
    int mid=lo+(hi-lo)/2;
    int index=lo;
    int head=lo;
    int tail=mid+1;

    for(int i=lo;i<=hi;++i){
    aux[i]=a[i];
    }

    while(index<=hi){
    if(head>mid){a[index++]=aux[tail++];}
    else if(tail>hi){a[index++]=aux[head++];}
    else if(less(aux[head],aux[tail])){a[index++]=aux[head++];}
    else{a[index++]=aux[tail++];}
    }
    }

    you may notice the aux array,it's defined as an inner variable of MergeSort class. 

    then it's time to sort a given array,we normally have two methods:the up2Down one and the down2Up one.

    the up2Down realization:

    public static void upDownMergeSort(Comparable[] a,int lo,int hi){
    if(hi<=lo) return;
    int mid=lo+(hi-lo)/2;
    upDownMergeSort(a,lo,mid);
    upDownMergeSort(a,mid+1,hi);
    updateMerge(a,lo,hi);
    }

    the down2Up realization:

    also this way has two methods:the complex method and a clear method

    at first ,I'll introduce the complex method.it's thinking is simpler ,so the numbers of code is greater.

    when the length of input array is the power of 2,the calculation is easy.the code is in the below:

    public static void innerDownUpMergeSort(Comparable[] a,int lo,int hi){
    if(hi<=lo) return;
    int len=hi-lo+1;
    int step=2;
    aux=new Comparable[len];
    int stages=0;
    while(step<=len){
    stages=Math.floorDiv(len,step);
    for(int i=0;i<stages;++i){
    merge(a,lo+i*step,lo+(i+1)*step-1);
    }
    step*=2;
    }
    }

    it works well when the input array length is 2's power,so we think that may be we can do something to change the common array into the special one with the length of 2^N. follow this way we can code as below:

    public static void downUpMergeSort(Comparable[] a,int lo,int hi){
    if(hi<=lo) return;
    int len=hi-lo+1;

    int N=len;
    if(0 != is2sPow((double)len)) {
    double lower = Math.log((double) len) / Math.log((double) 2);
    lower = Math.floor(lower)+1;
    N=(int)Math.pow(2,lower);
    }

    Comparable[] innerAux=new Comparable[N];
    for(int i=lo;i<=hi;++i){
    innerAux[i]=a[i];
    }
    for(int i=hi+1;i<N;++i){
    innerAux[i]=MAXINT;
    }

    innerDownUpMergeSort(innerAux,0,N-1);

    for(int i=lo;i<=hi;++i){
    a[i]=innerAux[i];
    }
    }

    then there is a function called is2sPow(),the explanation is showed in http://www.cnblogs.com/ssMellon/p/6423101.html

    it works ,but it's not clear.we can do it better.but unfortunately I found a function in a book called Algorithm decribed by Java.

    the following is the code:

    public static void updateDownUpMergeSort(Comparable[] a){
    int N=a.length;
    aux=new Comparable[N];
    for(int sz=1;sz<N;sz*=2){
    for(int lo=0;lo<N-sz;lo+=2*sz){
    merge(a,lo,Math.min(lo+2*sz-1,N-1));
    }
    }
    }

    So clear.

    The end.

  • 相关阅读:
    调试D2JS
    PG 中 JSON 字段的应用
    面试----
    机器学习面试题
    闭包和装饰器
    scss-混合@mixin @include @function
    scss基本使用及操作函数
    常用的scss函数(mixin)
    二叉搜索树基本操作实现
    判断一棵树是否是二叉搜索树
  • 原文地址:https://www.cnblogs.com/ssMellon/p/6536966.html
Copyright © 2011-2022 走看看