归并排序递归方式
public class Test11 {
// 归并排序
// 非递归
// 递归版本
public static void main(String[] args) {
int[] arr = new int[]{3,2,1,-1,231,31,31254};
mergeSort02(arr);
System.out.println(printArr(arr));
}
public static String printArr(int[] arr) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
stringBuilder.append(arr[i]);
stringBuilder.append(',');
}
return stringBuilder.toString();
}
public static void mergeSort1(int[] data){
msort(data,0,data.length-1);
}
public static void msort(int[] data, int start, int end){
if(start < end){
int mid = (start + end) >>> 1;
msort(data,start,mid);
msort(data,mid+1,end); //当mid+1 == end时,子序列长度为1,退出msort,进入merge()排序
merge(data,start,mid,end);
}
}
public static void merge(int[] data, int start, int mid, int end){
int s1 = start; //子序列1在data[]的起始下标
int s2 = mid + 1; //子序列2在data[]的起始下标
int[] tmp = new int[end - start +1];
int index = 0; //临时数组tmp的下标
while((s1 <= mid) && (s2 <= end)){ //两个序列都还没有完全放入tmp[]中
if(data[s1] < data[s2]) tmp[index++] = data[s1++];
else tmp[index++] = data[s2++];
}
while(s1 <= mid){ //第2个序列已经完全放入tmp中,由于s1本身是有序的,将s1剩下的元素直接放入
tmp[index++] = data[s1++];
}
while(s2 <= end){
tmp[index++] = data[s2++];
}
for(int i = 0; i < tmp.length; i++){
data[start++] = tmp[i];
}
}
// 非递归实现
public static void mergeSort02(int[] data) {
int k = 1;
while(k < data.length) {
merge(data, k);
k *= 2;
}
}
public static void merge(int[] data, int len) {
int s1 = 0;
int e1 = s1 + len-1;
int s2 = e1 + 1;
int e2 = s2 + len-1<data.length ? s2+len-1:data.length-1;
int[] tmp = new int[data.length];
int index = 0;
while(s2 < data.length) {
while((s1 <= e1) && (s2 <= e2)) {
if (data[s1] < data[s2]) tmp[index++] = data[s1++];
else tmp[index++] = data[s2++];
}
while(s1<=e1) {
tmp[index++] = data[s1++];
}
while(s2 <= e2) {
tmp[index++] = data[s2++];
}
s1 = e2 + 1;
e1 = s1 + len -1;
s2 = e1 + 1;
e2 = s2+ len - 1 < data.length? s2+len -1: data.length-1;
}
while (s1 < data.length) {
tmp[index++] = data[s1++];
}
for (int i = 0; i < data.length; i++) {
data[i] = tmp[i];
}
}
}