Java实现非递归归并排序
public class nonRecursiveMergeSort {
public static void main(String[] args) {
int[] list = {8,4,3,6,9};
MergeSort(list);
for(int num:list)
System.out.print(num);
}
public static void MergeSort(int[] list){
int len = 1;//2-路排序的实现
int[] temp = new int[list.length];
while(len<list.length){
MergePass(list, temp, list.length, len);
len = len*2;
if(len>=list.length){
for(int i =0;i<list.length;i++)
list[i] = temp[i];
}
else{
MergePass(temp, list, list.length, len);
len = len*2;
}
}
}
//两个数组归并为一个数组
public static void Merge(int[] list, int[] temp, int i, int m, int n){
int k = i, j = m+1;
for(;i<=m&&j<=n;k++){
if(list[i]<=list[j])
temp[k] = list[i++];
else{
temp[k] = list[j++];
}
}
while(i<=m)
temp[k++] = list[i++];
while(j<=n)
temp[k++] = list[j++];
}
public static void MergePass(int[] list, int[] temp, int n, int len){
int i =0;
while(i<=n-len*2){//处理两个长度相同子表
Merge(list, temp, i, i+len-1, i+len*2-1);
i = i+len*2;
}
if(i<n-len){//处理两个长度不同的子表
Merge(list, temp, i, i+len-1, n-1);
}
else{//不能构成两个子表
for(int j =i;j<n;j++){
temp[j] = list[j];
}
}
}
}