Heap SortBinary 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.
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. Representation of an array of the above figure is given below: 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 1. Max Heap: In a Binary Heap, for every node I other than the root, the value of the node is greater than or equal to the value of its highest child Thus, the highest element in a heap is stored at the root. Following is an example of MAXHEAP 2. MINHEAP: In MINHEAP, the value of the node is lesser than or equal to the value of its lowest child. Heapify Method:1. Maintaining the Heap Property: Heapify is a procedure for manipulating heap Data Structure. It is given an array A and index I into the array. The subtree rooted at the children of A [i] are heap but node A [i] itself may probably violate the heap property i.e. A [i] < A [2i] or A [2i+1]. The procedure 'Heapify' manipulates the tree rooted as A [i] so it becomes a heap. MAXHEAPIFY (A, i) 1. l ← left [i] 2. r ← right [i] 3. if l≤ heapsize [A] and A[l] > A [i] 4. then largest ← l 5. Else largest ← i 6. If r≤ heapsize [A] and A [r] > A[largest] 7. Then largest ← r 8. If largest ≠ i 9. Then exchange A [i] A [largest] 10. MAXHEAPIFY (A, largest) 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) HEAPSORT ALGORITHM:HEAPSORT (A) 1. BUILDMAXHEAP (A) 2. For I ← length[A] down to Z 3. Do exchange A [1] ←→ A [i] 4. Heapsize [A] ← heapsize [A]1 5. MAXHEAPIFY (A,1) Analysis: Build maxheap takes O (n) running time. The Heap Sort algorithm makes a call to 'Build MaxHeap' which we take O (n) time & each of the (n1) calls to Maxheap to fix up a new heap. We know 'MaxHeapify' takes time O (log n) The total running time of HeapSort is O (n log n). Example: Illustrate the Operation of BUILDMAXHEAP on the array. Solution: Originally: Priority Queue:As with heaps, priority queues appear in two forms: maxpriority queue and minpriority queue. A priority queue is a data structure for maintaining a set S of elements, each with a combined value called a key. A maxpriority queue guides the following operations: INSERT(S, x): inserts the element x into the set S, which is proportionate to the operation S=S∪[x]. MAXIMUM (S) returns the element of S with the highest key. EXTRACTMAX (S) removes and returns the element of S with the highest key. INCREASEKEY(S, x, k) increases the value of element x's key to the new value k, which is considered to be at least as large as x's current key value. Let us discuss how to implement the operations of a maxpriority queue. The procedure HEAPMAXIMUM consider the MAXIMUM operation in θ (1) time. HEAPMAXIMUM (A)1. return A [1] The procedure HEAPEXTRACTMAX implements the EXTRACTMAX operation. It is similar to the for loop of HeapSort procedure. HEAPEXTRACTMAX (A) 1 if A. heapsize < 1 2 error "heap underflow" 3 max ← A [1] 4 A [1] ← A [heapsize [A]] 5 heapsize [A] ← heapsize [A]1 6 MAXHEAPIFY (A, 1) 7 return max The procedure HEAPINCREASEKEY implements the INCREASEKEY operation. An index i into the array identify the priorityqueue element whose key we wish to increase. HEAPINCREASEKEY.A, i, key) 1 if key < A[i] 2 errors "new key is smaller than current key" 3 A[i] = key 4 while i>1 and A [Parent (i)] < A[i] 5 exchange A [i] with A [Parent (i)] 6 i =Parent [i] The running time of HEAPINCREASEKEY on an nelement 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 MAXHEAPINSERT implements the INSERT operation. It takes as an input the key of the new item to be inserted into maxheap A. The procedure first expands the maxheap by calculating to the tree a new leaf whose key is  ∞. Then it calls HEAPINCREASEKEY to set the key of this new node to its right value and maintain the maxheap property MAXHEAPINSERT (A, key) 1 A. heapsize = A. heapsize + 1 2 A [A. heapsize] =  ∞ 3 HEAPINCREASEKEY (A, A. heapsize, key) The running time of MAXHEAPINSERT on an nelement heap is O (log n). Example: Illustrate the operation of HEAPEXTRACTMAX on the heap Fig: Operation of HEAPINCREASEKEY Fig: (a) In this figure, that maxheap with a node whose index is 'i' heavily shaded Fig: (b) In this Figure, this node has its key increased to 15. Fig: (c) After one iteration of the while loop of lines 46, the node and its parent have exchanged keys, and the index i moves up to the parent. Fig: (d) The maxheap after one more iteration of the while loops, the A [PARENT (i) ≥A (i)] the maxheap property now holds and the procedure terminates. HeapDelete:HeapDELETE (A, i) is the procedure, which deletes the item in node 'i' from heap A, HEAPDELETE runs in O (log n) time for nelement max heap. HEAPDELETE (A, i) 1. A [i] ← A [heapsize [A]] 2. Heapsize [A] ← heapsize [A]1 3. MAXHEAPIFY (A, i)
Next TopicQuick Sort

PLC
Illustrator
Postman
IoT
GraphQL
Xampp
Kivy
A. Anywhere
Ext.js
UiPath
Arduino
Digital E.
Adwords
MySQL