There are three cases to consider: 1 After deletion, there are no elements remaining in level L. This occurs once in processing level L. In this case, the farthest point to which the root can be sifted is to level L - 1. Therefore, the maximum number of comparisons here is 2 L - 2. This also occurs once in processing level L. In this case, the farthest point to which the root can be sifted is the single spot in level L. There are 2 L - 1 comparisons to sift the element to the bottom level.
Steven J. Zeil
When the item being sifted reaches the bottom level, it must go to a position that has a brother, otherwise the maximum number of comparisons will not be achieved. We will refer to this upper bound as uby. Now, we must determine how close to UB the actual worst n case is. Given a permutation of 1, 2, The entire L level is considered to be currently deleted from the tree. For each element, x, in level L, proceeding from left to right, unsift an element, y, to the root, switch the root, y, with x and consider the position now containing y to be in the tree.
Upon sifting, we must be sure to choose an appropriate element so that when the root is switched with the deleted element in the bottom level, the element will be small enough to preserve a heap.
Furthermore, in order to achieve the upper bound described, elements must be unsifted from one of two possible positions: 1 the bottom level the bottom is level L - 1 the first time and level L the remainder of the time , where the node in that position has a brother that is not deleted from the tree, or 2 the next to the bottom level, where the node in that position has two sons, both of which - 11 - are not deleted from the tree. In this case, simply unsift that node or its father regardless of the fact that it does not have a brother or two sons in the tree.
The circled numbers refer to the node being unsifted, the dotted circles represent alternative choices that also lead to worst-case heaps and the cut-off portions of the trees are the nodes currently deleted from the tree. Observe that in the starred tree, had the 1 been chosen for unsifting, then upon going forward, when the 1 would have been sifted, there would have been only one com- parison at the bottom level since the node where the 1 lies does not have a brother. Unfortunately, this does not work as a position is obtained in which no possible node exists in the bottom or next to bottom level for unsifting as previously described.
We must immediately choose the 1 for unsifting, for if we do not, then after some unsift operations, the 1 would be the father of the next node to be restored to the tree.
There- fore, there would be no possible element to be unsifted since any potential choice would be greater than 1 and would have to be switched to the position under the 1, creating a tree that is not a heap i. However, if the 1 is the first element unsifted, then the 2 cannot be chosen after that, without a loss of comparisons from ub-]-: As with the 1, after some unsift operations, the 2 would be the father of the next node to be restored to the tree.
In this case, only the 1 could be unsifted and then switched to the bottom level, becoming the left son of the 2. The next node to be restored to the tree would be the right son of the 2. Unfor- tunately, the only elements that could be unsifted and switched into that position would be the 1, which currently has no brothers, or the 2 which only has one son the 1.
Since the 1 is unsifted from the bottom level no comparisons are lost. If it does not have a brother, then the 2 can be unsifted, also from the bottom level. In either case, the 1 or 2 is small enough to preserve a heap, since the smallest element in level L - 1 is bigger than 2.
Furthermore, if this is to be accomplished, then it is impossible for the 2 to remain in the leftmost position of level L. The 1 is now the left son of the 2 and it is the only element in level L. Unsift the 1 again unsifting the 2 would work also. Upon unsifting the 1, the 2 falls into level L. Repeat above steps while size of heap is greater than 1.
How to build the heap? Heapify procedure can be applied to a node only if its children nodes are heapified. So the heapification must be performed in the bottom up order. Notes: Heap sort is an in-place algorithm. Its typical implementation is not stable, but can be made stable See this.
Time Complexity: Time complexity of heapify is O Logn. Applications of HeapSort 1. Sort a nearly sorted or K sorted array 2. Heap sort algorithm has limited uses because Quicksort and Mergesort are better in practice. Nevertheless, the Heap data structure itself is enormously used.
See Applications of Heap Data Structure. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Writing code in comment? Table of Contents. Abstract PDF. Ulrich Laube , Markus E.
This Paper Appears in. Title Information. Series: Proceedings. Abstract We present a new approach for an average-cases analysis of algorithms that supports a non-uniform distribution of the inputs and is based on the maximum likelihood training of stochastic grammars.