简体   繁体   English

为什么堆排序的时间复杂度是 O(nlogn)?

[英]Why is the time complexity of Heap Sort, O(nlogn)?

I'm trying to understand time complexity of different data structures and began with heap sort.我试图了解不同数据结构的时间复杂度,并从堆排序开始。 From what I've read, I think collectively people agree heap sort has a time complexity of O(nlogn);从我读到的内容来看,我认为人们普遍同意堆排序的时间复杂度为 O(nlogn); however, I have difficulty understanding how that came to be.然而,我很难理解这是如何发生的。

Most people seem to agree that the heapify method takes O(logn) and buildmaxheap method takes O(n) thus O(nlogn) but why does heapify take O(logn)?大多数人似乎都同意 heapify 方法需要 O(logn) 而 buildmaxheap 方法需要 O(n) 因此 O(nlogn) 但是为什么 heapify 需要 O(logn)?

From my perspective, it seems heapify is just a method that compares a node's left and right node and properly swaps them depending if it is min or max heap.从我的角度来看,似乎 heapify 只是一种比较节点的左右节点并根据它是最小堆还是最大堆来正确交换它们的方法。 Why does that take O(logn)?为什么需要 O(logn)?

I think I'm missing something here and would really appreciate if someone could explain this better to me.我想我在这里遗漏了一些东西,如果有人能更好地向我解释这一点,我将不胜感激。

Thank you.谢谢你。

It seems that you are confusing about the time complexity about heap sort.您似乎对堆排序的时间复杂度感到困惑。 It is true that build a maxheap from an unsorted array takes your O(n) time and O(1) for pop one element out.确实,从未排序的数组构建 maxheap 需要 O(n) 时间和 O(1) 来弹出一个元素。 However, after you pop out the top element from the heap, you need to move the last element(A) in your heap to the top and heapy for maintaining heap property.但是,从堆中弹出顶部元素后,您需要将堆中的最后一个元素(A)移动到顶部和堆中以保持堆属性。 For element A, it at most pop down log(n) times which is the height of your heap.对于元素 A,它最多弹出 log(n) 次,即堆的高度。 So, you need at most log(n) time to get the next maximum value after you pop maximum value out.因此,在弹出最大值后,您最多需要 log(n) 时间来获得下一个最大值。 Here is an example of the process of heapsort.下面是一个堆排序过程的例子。

          18 

      15        8  

   7     11   1    2  

 3   6 4    9

After you pop out the number of 18, you need to put the number 9 on the top and heapify 9.弹出18这个数字后,需要把数字9放在最上面,堆成9。

          9

      15       8

   7     11  1   2

 3   6 4

we need to pop 9 down because of 9 < 15我们需要弹出 9 因为 9 < 15

             15

          9       8

       7     11  1   2

     3   6 4

we need to pop 9 down because of 9 < 11我们需要弹出 9 因为 9 < 11

          15

      11       8

   7     9  1   2

 3   6 4

9 > 4 which means heapify process is done. 9 > 4 这意味着 heapify 过程完成。 And now you get the next maximum value 15 safely without broking heap property.现在您可以安全地获得下一个最大值 15,而不会破坏堆属性。

You have n number for every number you need to do the heapify process.对于执行 heapify 过程所需的每个数字,您都有 n 个数字。 So the time complexity of heapsort is O(nlogn)所以堆排序的时间复杂度是O(nlogn)

You are missing the recursive call at the end of the heapify method.您错过了 heapify 方法末尾的递归调用。

Heapify(A, i) {
   le <- left(i)
   ri <- right(i)
   if (le<=heapsize) and (A[le]>A[i])
      largest <- le
   else
      largest <- i 
   if (ri<=heapsize) and (A[ri]>A[largest])
      largest <- ri
   if (largest != i) {
      exchange A[i] <-> A[largest]
      Heapify(A, largest)
   }
}

In the worst case, after each step, largest will be about two times i .在最坏的情况下,在每一步之后, largest大约是i两倍。 For i to reach the end of the heap, it would take O(logn) steps.要让 i 到达堆的末尾,需要O(logn)步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM