我编写了一个就地合并排序算法,用于对大量随机大小(100,000 个元素或更多)的数据进行排序。我正在考虑在数据几乎排序时进行插入排序,以使算法运行得更快一点。我想知道这是否可能与就地合并排序?
这是我的一些代码。
public static void merge(ArrayList<String> list, int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
merge(list, low, mid);
merge(list, mid + 1, high);
mergeSort(list, low, mid, high);
}
}
public static void mergeSort(ArrayList<String> list, int first, int mid,
int last) {
int left = first;
int right = mid + 1;
String holder = "";
// if mid <= mid+1 skip merge
if (compareTo(list.get(mid), list.get(right)) <= 0) {
return;
}
while (left <= mid && right <= last) {
// if left index <= right index then just add to left
if (compareTo(list.get(left), list.get(right)) <= 0) {
left++;
} else {
holder = list.get(right);
copyList(list, left, right - left);//moves everything from left to right-left up one index in the arraylist
list.set(left, holder);
left++;
mid++;
right++;
}
}
// what is left is in place
}
public static void copyList(ArrayList<String> source, int srcPos, int length) {
String temp1 = "";
String temp2 = source.get(srcPos);
for (int i = 0; i < length; i++) {
temp1 = source.get(srcPos + 1);
source.set(srcPos + 1, temp2);
temp2 = temp1;
srcPos++;
}
}
现在,当我第一次将元素放入 arraylist 时,我正在考虑通过计数器的数量来实现插入排序,然后将我的合并方法更改为以下。
public static void merge(ArrayList<String> list, int low, int high) {
if(high-low==dataSize-1){
int mid = (low + high) / 2;
merge(list, low, mid);
merge(list, mid + 1, high);
insertionSort(list);
}else if (low < high) {
int mid = (low + high) / 2;
merge(list, low, mid);
merge(list, mid + 1, high);
mergeSort(list, low, mid, high);
}
}
但是,这实际上使我的算法具有永恒性。我猜我做错了,算法需要 n^2 来运行,因为数据是完全随机生成的,而且几乎没有排序。
我究竟做错了什么?有什么建议么?我的猜测是因为它就地合并排序它不会工作。
谢谢!