This document describes heap data structures and algorithms like heap sort. It defines a max heap and min heap. It explains the build heap, heapify, insertion and deletion algorithms. Build heap transforms an array into a max heap by applying heapify to each node from bottom to top. Heapify maintains the heap property when a node is added or removed. Heap sort works by building a max heap from the input array and then extracting elements from the root to sort the array in descending order.
3. Binary heap
A Binary heap is a complete binary tree in which every
node satisfies the heap property which states that :
i. If B is a child of A, then key(A)≥ key (B)
This implies that elements at every node will be greater
than or equal to the element at its left or right child. As
such, the root node has the highest value in the heap.
Such a heap is called a Max-heap
4. ii.
Alternatively, elements at every node will either be less than or
equal to the element at its left and its right child. Thus the root
has the lowest key value. Such a heap is called a Min heap
7. priority queues
Graph algorithms like Dijkstra’s algorithm
Order statistics
applications
8. How to Build a heap
Algorithm
Build_Max_Heap (A)
1. A.Heap_size = A.length
2. For i = A.length/2 down to 1
3. Max_heapify(A,i)
A- an Unsorted Array
i- Array index
15. Heapify is nothing but the procedures of heap.
Heapify is a procedure for manipulating heap data
structures.
It is given an array A and index i into the array. The sub tree
rooted at the children of A[i] are heap but node A[i] itself
may possibly violate the heap property i.e., A[i] < A[2i] or
A[i] < A[2i +1]. The procedure 'Heapify' manipulates the
tree rooted at A[i] so it becomes a heap. In other words,
'Heapify' is let the value at A[i] "float down" in a heap so
that sub tree rooted at index i becomes a heap.
What is heapify ?
16. Convert an unordered integer array into a heap array.
Rearrange a heap to maintain heap property that is
the key of the root node is more extreme (greater or
less) than or equal to the keys of its children.
What does Heapify?
17. Heapify picks the largest child key and compare it to
the parent key. If parent key is larger than heapify
quits, otherwise it swaps the parent key with the
largest child key. So that the parent is now becomes
larger than its children.
Procedure outline of heapify
19. If we put a value at root that is less than every value in the
left and right subtree, then 'Heapify' will be called
recursively until leaf is reached. To make recursive calls
traverse the longest path to a leaf, choose value that make
'Heapify' always recurse on the left child. It follows the left
branch when left child is greater than or equal to the right
child, so putting 0 at the root and 1 at all other nodes, for
example, will accomplished this task. With such values
'Heapify' will called h times, where h is the heap height so
its running time will be θ(h) (since each call does (1) work),
which is (lgn). Since we have a case in which Heapify's
running time (lg n), its worst-case running time is Ω(lgn).
Analysis of heapify
20. For an array implementation heapify takes O(log n)
time under the comparison model, where n is the
number of nodes & h is the height.
Time and space complexity of
heapify
21. Insertion
Example, Build a max heap H from the given set of
numbers: 56, 23, 28, 56, 72, 64
(step 1) (step 2) (step 3)
56 56
23
56
23 28
28. HEAP SORT
HEAP SORT is a comparison-based sorting algorithm . Heap sort can be
thought of an improved Selection sort , it divides its input into a sorted and an
unsorted region , and it iteratively shrinks the unsorted region by extracting
the largest element and moving that to the sorted region.
HEAPSORT is an in-place algorithm , but it is not a stable sort.
55. conclusion
The primary advantage of the heap sort is its efficiency. The
execution time efficiency of the heap sort is O(n log n). The
memory efficiency of the heap sort, unlike the other n log n
sorts, is constant, O(1), because the heap sort algorithm is not
recursive.
The heap sort algorithm has two major steps. The first major
step involves transforming the complete tree into a heap. The
second major step is to perform the actual sort by extracting
the largest element from the root and transforming the
remaining tree into a heap
56. http://www.wikipedia.org/heap_sort
Thomas H. C,Charles E. L. (2009)Introduction to
algorithms. Third Edition, ISBN 0-07-013151-1 (McGraw-
Hill)
http://www.cs.rit.edu/heap_sort
Reference