Inspired by this post, I googled the worst case of heapsort and found this question on cs.stackexchange.com, but the only answer didn't really answer the question, so I decided to dig it out myself. After hours of reasoning and coding, I've solved it. and I think this question belongs better in SO, so I post it up here.
The problem is to find a heapified array containing different numbers from 1 to n, such that when converting it to a sorted array, the total number of exchanges in all sifting operations is maximal possible.
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Finding k smallest elements in a min heap - worst-
- Index of single bit in long integer (in C) [duplic
Of course there is a brute force algorithm which calculates all possible of the heapified arrays and counts the number of exchanges for each one, and I have done that to verify the result of the solution below.
let's start from N=1: 1
N=2: apparently, it's [2, 1]
N=3: [3, x, 1].
Since each sifting call will incur, at most, a number of swaps equal to the "height(which is equal to ⌊log(n)⌋" (from the bottom of the heap) of the node on which the sifting call is made, so we place 1 to the end of the array. apparently, x=2.
N=4: [4, x, y, 1]
After first extract-max, we need heapify [1, x, y]. If we sift it to the case when N=3, [3, 2, 1], since this sifting operation incurs the most swaps which is equal to the "height", plus the maximal number of exchanges when N=3, so that's the scenario of maximal number of exchanges when N=4. Thus, we do the "siftDown" version of heapify backwards to [3, 2, 1]: swap 1 with its parent until 1 is the root. So x=2, y=3
N = n: [n,a,b,c,...,x,1]
So, by induction, we do the same thing when N=n: after first extract-max, we sift down [1, a, b, c, ..., x] to the heapified array when N= n-1. So we do this backwards, get what we what.
Here is the code that outputs the heapified array which meets the requirements when you input N: