Merge Sort

Posted 空山皓月

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.

以上是关于Merge Sort的主要内容,如果未能解决你的问题,请参考以下文章

归并排序(Merge Sort)思想,代码实现

自底向上归并排序(Merge Sort)

排序算法之归并排序(Merge Sort)

python [代码片段]一些有趣的代码#sort

归并排序(Merge sort)

归并排序(merge_sort)