所以我在弄乱排序的实现,并且发现尝试使用ArrayLists和简单的Binary Search这样的基本实现不会有什么坏处:
public static ArrayList<Integer> binarySort(ArrayList<Integer> list) {
ArrayList<Integer> sortedList = new ArrayList<>();
for(Integer value : list) {
int index = binarySearchPosition(sortedList, value);
sortedList.add(index, value);
}
return sortedList;
}
public static int binarySearchPosition(ArrayList<Integer> list, Integer value) {
int min = 0;
int max = list.size();
while(max - min > 1) {
int mid = (int) Math.floor((max + min) / 2);
if(list.get(mid) < value) {
min = mid;
} else {
max = mid;
}
}
if(max == 0) {
return 0;
}
if(list.get(min) < value) {
return max;
} else {
return min;
}
}
它的行为本质上与HeapSort相同,但实际上不会从数据中创建堆。这样的东西会被定义为HeapSort还是其他形式?
list.add(index, value)
本身是O(n)运算。从统计上讲,人们希望它移动N个元素的1/2,big-O表示法将丢弃1/2个元素。]所以最后您要进行的操作是O(n ^ 2 * log2(n)),它比O(n ^ 2)慢。
由于没有数学依据,因此大致可分为:
N个元素一次添加一个O(n)。
移动N个元素O(n)的1/2。
二进制搜索以找到插入索引O(log2(n))。
请注意,如果您知道已经达到O(n ^ 2),可以通过一种非常简单的算法避免寻找索引的额外工作:
Create a new array one element bigger.
for each element in the original array {
if the element is smaller than the added item, copy it at the same index.
if the element is same / larger than the added item, copy in the added item, and copy the rest of the elements from index to index+1.
}
现在堆是另一回事了;但是,您知道这是因为它们不会产生排序列表。