## Heap Sort## Binary Heap:Binary Heap is an array object can be viewed as Complete Binary Tree. Each node of the Binary Tree corresponds to an element in an array. - Length [A],number of elements in array
- Heap-Size[A], number of elements in a heap stored within array A.
The root of tree A [1] and gives index 'i' of a node that indices of its parents, left child, and the right child can be computed.
The index of 20 is 1 To find the index of the left child, we calculate 1*2=2 This takes us (correctly) to the 14. Now, we go right, so we calculate 2*2+1=5 This takes us (again, correctly) to the 6. Now, 4's index is 7, we want to go to the parent, so we calculate 7/2 =3 which takes us to the 17. ## Heap Property:A binary heap can be classified as Max Heap or Min Heap
Thus, the highest element in a heap is stored at the root. Following is an example of MAX-HEAP
## Heapify Method:
## Analysis:The maximum levels an element could move up are Θ (log n) levels. At each level, we do simple comparison which O (1). The total time for heapify is thus O (log n). ## Building a Heap:BUILDHEAP (array A, int n) 1 for i ← n/2 down to 1 2 do 3 HEAPIFY (A, i, n) ## HEAP-SORT ALGORITHM:
## Priority Queue:As with heaps, priority queues appear in two forms: max-priority queue and min-priority queue. A priority queue is a data structure for maintaining a set S of elements, each with a combined value called a key. A max-priority queue guides the following operations:
Let us discuss how to implement the operations of a max-priority queue. The procedure HEAP-MAXIMUM consider the MAXIMUM operation in θ (1) time. ## HEAP-MAXIMUM (A)1. return A [1] The procedure HEAP-EXTRACT-MAX implements the EXTRACT-MAX operation. It is similar to the for loop of Heap-Sort procedure.
The procedure HEAP-INCREASE-KEY implements the INCREASE-KEY operation. An index i into the array identify the priority-queue element whose key we wish to increase.
The running time of HEAP-INCREASE-KEY on an n-element heap is O (log n) since the path traced from the node updated in line 3 to the root has length O (log n). The procedure MAX-HEAP-INSERT implements the INSERT operation. It takes as an input the key of the new item to be inserted into max-heap A. The procedure first expands the max-heap by calculating to the tree a new leaf whose key is - ∞. Then it calls HEAP-INCREASE-KEY to set the key of this new node to its right value and maintain the max-heap property
In this figure, that max-heap with a node whose index is 'i' heavily shaded
In this Figure, this node has its key increased to 15.
After one iteration of the while loop of lines 4-6, the node and its parent have exchanged keys, and the index i moves up to the parent.
The max-heap after one more iteration of the while loops, the A [PARENT (i) ≥A (i)] the max-heap property now holds and the procedure terminates. ## Heap-Delete:Heap-DELETE (A, i) is the procedure, which deletes the item in node 'i' from heap A, HEAP-DELETE runs in O (log n) time for n-element max heap.
Next TopicQuick Sort |