Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm HeapSort中已降序排序数组的时间复杂度_Algorithm_Sorting_Time Complexity_Analysis_Heapsort - Fatal编程技术网

Algorithm HeapSort中已降序排序数组的时间复杂度

Algorithm HeapSort中已降序排序数组的时间复杂度,algorithm,sorting,time-complexity,analysis,heapsort,Algorithm,Sorting,Time Complexity,Analysis,Heapsort,考虑已按降序排序的数组A[n]。堆已经建立。现在考虑一个循环,在这个循环中,我们将[1 ](数组索引从1开始)与[Heop.Sime]交换。以下是伪代码: Build-Max-Heap(A) //Already done while (i > 0) { swap(A[1] with A[heap_size] heap_size = heap_size - 1 Max-Heapify(A,1) //Takes lg(A.heap_size) time to f

考虑已按降序排序的数组A[n]。堆已经建立。现在考虑一个循环,在这个循环中,我们将[1 ](数组索引从1开始)与[Heop.Sime]交换。以下是伪代码:

Build-Max-Heap(A) //Already done
while (i > 0) {

     swap(A[1] with A[heap_size]
     heap_size = heap_size - 1
     Max-Heapify(A,1) //Takes lg(A.heap_size) time to float the 1st element down to it's respective position

} 

我们在元素1上调用Max Heapify来恢复heap属性,允许它向下浮动到适当的位置。我们知道Max Heapify需要clg(n)时间。那么,循环不应该花费c(lg(n)+lg(n-1)+..+lg(1))=θ(log(n))时间而不是jutθ(n*lg(n))?因为堆大小随着每次迭代而减小?

n..1的对数之和不是
log(n)
,而是
nlogn
(查看斯特林公式)


从任意数组构建的经典堆是O(n)进程-不是O(nlogn)

n的对数之和。1不是
log(n)
,而是
nlogn
(查看斯特林公式)


从任意数组生成的经典堆是O(n)进程-不是O(nlogn)

同意2)和3),而是按堆排序的数组。@SaiBot OK,removed.Oops。在日志中添加n…1项而不是将它们相乘,这是一个错误!谢谢同意2)和3),但排序数组是按堆排序的。@SaiBot OK,removed.Oops。在日志中添加n…1项而不是将它们相乘,这是一个错误!谢谢