package com.lm.digui;
import java.util.Arrays;
public class MergonQuery {
public static void main(String[] args) {
int merget[] = {2, 5, 39, 2, 2, 4, 2, 0, 74, 7, 5, 6, 3, 2, 1};
System.out.println(Arrays.toString(megerSort(merget)));
}
private static int[] megerSort(int[] merget) {
if (merget.length < 2) {
return merget;
}
int mid = merget.length / 2;
int[] left = Arrays.copyOfRange(merget, 0, mid);
int[] after = Arrays.copyOfRange(merget, mid, merget.length);
return sertNumber(megerSort(left), megerSort(after));
}
private static int[] sertNumber(int[] left, int[] after) {
int[] result = new int[left.length + after.length];
for (int index = 0, i = 0, j = 0; index < result.length; index++) {
if (i >= left.length) {
result[index] = after[j++];
} else if (j >= after.length) {
result[index] = left[i++];
} else if (left[i] > after[j]) {
result[index] = after[j++];
} else {
result[index] = left[i++];
}
}
return result;
}
}
原文:https://www.cnblogs.com/limingming1993/p/12154001.html