310 likes | 320 Views
Heapsort. Lecture 4 Asst. Prof. Dr. İlker Kocabaş. Sorting Algorithms. Insertion sort Merge sort Quicksort Heapsort Counting sort Radix sort Bucket sort Order statistics. Heapsort.
E N D
Heapsort Lecture 4 Asst. Prof. Dr. İlker Kocabaş
Sorting Algorithms • Insertion sort • Merge sort • Quicksort • Heapsort • Counting sort • Radix sort • Bucket sort • Order statistics
Heapsort • The binary heap data structure is an array object that can be viewed as a complete binary tree. Each node of the tree corresponds to an element of the array that stores the value in the node. • An array A[1 ... n] that represents a heap is an object with two attributes: length[A] : The number of elements in the array heap-size[A] : The number of elements in the heap stored within the arrayA heap-size[A]≤ length[A].
Heaps • The root of the tree is A[1]. • Given the indexi of a node, the indices of its parent and children can be computed as PARENT(i) return LEFT(i) return2i RIGHT(i) return2i+1
Heap property • Two kinds of heap: Maximum heap and minimum heap. • Max-heap property is that for every node iother than the root • Min-heap property is that for every node iother than the root
Heap property In both kinds, the values in the nodes satisfy the corresponding property (max-heap and min-heap properties)
Heap property 1 16 3 2 10 14 4 5 6 7 8 7 9 5 8 9 10 11 3 4 1 4 4 A max-heap viewed as a binary tree
Heap property • Weneedto be precise in specifyingwhetherweneed a max-heapor a min-heap. • Forexamplemin-heapsarecommonlyused in priorityqueues. • We define theheight of a node in a heapto be thenumber of edges on thelongestsimpledownwardpathfromthenodeto a leaf. • We define theheight of theheapto be theheight of itsroot. • Theheight of a heap is Ω(lg n).
Heap property Inthischapterwewillconsiderthefollowingbasicprocedures: • TheMAX-HEAPIFYwhichruns in O(lgn), is thekeytomaintainingthemax-heap. • TheBUILD-MAX-HEAPwhichruns in O(n), produces a maxheapfromunorderedinputarray. • TheHEAP-SORTwhichruns in O(nlgn), sorts an array in place. • Procedureswhichallowtheheap data structureto be used as priorityqueue. • MAX-HEAP-INSERT • HEAP-EXTRACT-MAX • HEAP-INCREASE-KEY • HEAP-MAXIMUM
Maintaining the heap property MAX-HEAPIFY is an important subroutine for manipulating max-heaps. When MAX-HEAPIFY(A,i) is called it forces the value A[i] “float down” in the max heap so that the the subtree rooted at index i becomes a max heap.
The Running Time of MAX-HEAPIFY The running time of MAX-HEAPIFY • Fix up the relationships among the parent and children • Calling for MAX-HEAPIFY : The children’s subtrees each have size at most 2n/3 (The worst case occurs when the last row of the tree is exactly half full)
Building a heap We note that the elements in the subarray are all leaves of the tree. Therefore the procedure BUILD-MAX-HEAP goes through the remaining nodes of the tree and runs MAX-HEAPIFY on each one
Buiding a Heap (contd.) Running time: • Each call to MAX-HEAPIFY costs O(lg n) time, • There are O(n) such calls. •The running time is (not asymptotically tight.)
Buiding a Heap (contd.) Asymptotically tight bound: We note that heights of most nodes are small. An n-elementheap has height lg n At mostn/2h+1 nodes of any height h.
Buiding a Heap (contd.) 1 Maximum number of nodes at height h (n=11) 16 3 2 10 14 4 5 6 7 8 7 9 5 8 9 10 11 3 4 1 4 4
Buiding a Heap (contd.) Asymptotically tight bound (cont.) Time required by MAX_HEAPIFY when called on a node of height h isO(h)
Priority queues • Heapsort is an excellent algorithm, but a good implementation of quicksort usually beats it in practice. • The heap data structure itself has an enormous utility. • One of the most popular applications of heap: its use as an efficient priority queue.
Priority queues(contd.) • A priority queue is a data structure for maintaining a set S of elements, each with an associated value called a key. • A max-priority queue suppports the following operations: INSERT(S,x) inserts the element x into the set S. MAXIMUM(S) returns the element of S with the largest key. EXTRACT-MAX(S) removes and returns the element of S with the largest key INCREASE-KEY(S,x,k) increases the value of element x’s key to the new value k, which is assumed to be k ≥ x.
The MAXIMUM operation The procedureHEAP-MAXIMUM implements the MAXIMUM operation HEAP-MAXIMUM(A) 1 returnA[1] T(n)=Θ(1)
The EXTRACT-MAX operation HEAP-EXTRACT-MAX removes and returns the element of the array A with the largest key HEAP-EXTRACT-MAX(A) 1 if heap-size[A]<1 2 then error “heap underflow” 3 max←A[1] 4 A[1]←A[ heap-size[ A]] 5 heap-size[ A]←heap-size[ A]-1 6 MAX-HEAPIFY(A,1) 7 return max
The INCREASE-KEY operation HEAP-INCREASE-KEY increases the value of element x’s key to the new value k which is greater than or equal to x. HEAP-INCREASE-KEY(A, i, key) 1 if key < A[i] 2 then error “new key is smaller than current key” 3 A[i]←key 4 while i >1 and A[ PARENT(i)] < A[i] 5 do exchange A[i] ↔A[ PARENT(i)] 6 i ← PARENT(i)
The operation of HEAP-INCREASE-KEY Increasing the key=4 to15
The INSERT Operation The MAX-HEAP-INSERT implements the insert operation. MAX-HEAP-INSERT(A, key) 1 heap-size[A]← heap-size[A]+1 2 A[ heap-size[ A]]← - 3 HEAP-INCREASE-KEY(A, heap-size[A], key)