heapsort input with most and fewest comparisons

你。 提交于 2019-12-01 22:58:41
jfly

Heap sort is different from bubble sort and quick sort, the best and worst cases wouldn't happen when the input elements are ordered in a descending/ascending manner. The first step of heap sort is building a heap(max-heap in general) which can be done in linear time O(n) by using the "sift down" version of heapify, no matter what order the initial elements are. If the input is already a heap, it just saves the time for exchange.
It's generally considered the best and worst cases performance are both O(nlogn)(the heap sort item on wiki says the best case performance can be Ω(n), and there's a link about it), but the big-O notation omits constant factors and lower order terms, so they are equivalent in big O notation, but they can differ by a constant factor.

For example, I get all 720 permutations of a given elemnts:{1,2,3,4,5,6} and sort them with your code, the minimum/maximum number of comparisons is 12/17 while the order is {6,1,4,2,3,5}/{1,4,2,5,6,3} respectively. And the minimum/maximum number of exchanges is 8/15 while the order is {6,3,5,1,2,4}/{1,2,3,5,4,6} respectively. My another post is about the maximum number of exchanges.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!