public static int[] k_merge(ArrayList<int[]> k_array) {
if(CollectionUtils.isEmpty(k_array)){
return new int[0];
}
int size = k_array.size();
int k = size;
ArrayList<int[]> objects = null;
while (k > 1) {
objects = Lists.newArrayList();
for (int i = 0; i < k; i += 2) {
if (i + 1 < k) {
int[] ints = m_merge(k_array.get(i), k_array.get(i + 1));
objects.add(ints);
} else {
objects.add(k_array.get(i));
}
}
k_array.clear();
for(int i=0;i<objects.size();i++){
k_array.add(objects.get(i));
}
k = k_array.size();
}
return objects.get(0);
}
public static int[] m_merge(int[] a, int[] b) {
int a_length = a.length;
int b_length = b.length;
int[] result = new int[a_length + b_length];
int lpos = 0;
int rpos = 0;
for (int i = 0; i < result.length; i++) {
if (lpos < a_length && rpos < b_length) {
if (a[lpos] <= b[rpos]) {
result[i] = a[lpos++];
} else {
result[i] = b[rpos++];
}
} else if (lpos < a_length) {
result[i] = a[lpos++];
} else if (rpos < b_length) {
result[i] = b[rpos++];
}
}
return result;
}