SlideShare a Scribd company logo
1 of 57
Heap
Mamotlatsi cloudia Seotsa 37
Saddam Hussain 29
Jyotishna Bora 12
contents
1. Binary heap description
2. Applications of heaps
3. Build heap algorithm
4. Heapify alorithm
4.1 Insertion
4.2 Deletion
5. Heap Sort algorithm
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
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
Max heap
95
69 72
45 63 21 43
26 32
Min heap
5
7 11
8 9 21 45
10
 priority queues
 Graph algorithms like Dijkstra’s algorithm
 Order statistics
applications
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
Example
Consider values: 10, 7, 6, 1, 9, 3, 2 , 4
Complete binary tree Representation
.
10
67
4
9 31 2
Step 1: Apply heapify(A,4)
1
2 3
i=4 5 6 7
8
10
67
4
9 31 2
Step 1: Apply heapify(A,3)
1
2 i= 3
4 5 6 7
8
10
67
1
9 34 2
Step 1: Apply heapify(A,2)
1
i = 2 3
4 5 6 7
8
10
67
1
9 34 2
Step 1: Apply heapify(A,1)
i=1
2 3
4 5 6 7
8
10
69
1
7 34 2
 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 ?
 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?
 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
Understanding the heapify algorithm
with an example
 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
 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
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
56, 23, 28, 51, 72, 64
(step 4) (Step 5)
56
23 28
51
51
56, 23, 28, 51, 72, 64
(step 6) (step 7)
56
51 28
23 72
56
2872
23 51
56, 23, 28, 51, 72, 64
(step 8) (step 9)
72
56
28
21 51 64
72
56 64
21 51
28
Array representation
72 56 64 21 51 28
Deletion example
Consider a Max heap H shown below and delete the root node
(step 1)
54
45 36
27 29 18 21
11
11
45 36
27 1829 21
(Step 3) (Step 4)
45
11 36
27 29 18 21
45
29
11
36
1827 21
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.
HEAPSORT ALGORITHM
HEAPSORT(A)
1. BUILD‐MAX‐HEAP(A)
2. for i ← length[A] downto 2
3. do exchange A[1] A[i]
4. heap‐size[A] ← heap‐size[A] −1
5. MAX‐HEAPIFY(A, 1)
Example:
Considering A={9,4,5,8,10,16,7,2,1,14} a list of elements. Apply heap sort
to sort these element..
Solution:
A={9,4,5,8,10,16,7,2,1,14}
Step1:Build a heap
9 9
4
9
4 5
9
4 5
8
9
8 5
4 10
10
9 5
4 8
10
9 5
4 8 16
16
109
4 8 5 7
2 1 14
16
14 10
4 9
2 1 8
5 7
1
2 3
4
5
6
7
8 9
10
Array representration
16 14 10 4 9 5 7 2 1 8
1 2 3 4 5 6 7 8 9 10
A
Step 2: for i = n to 1 do
Swap( A[1],A[i])
8
14 10
4 9 5 7
2 1 16
Swap(A[1],A[10])
Swap(16,8)
Step 3: Set n n-1
Here n=10
n=10-1=9
8
14 10
4 9 5 7
2 1
1
2
3
4
5 6
7
8 9
Step 4 : Heapify(A ,1)
i.e. Again set the heap property
14
9 10
4 8 5 7
2 1
Swap(A[1],A[i])
Swap(A[1],A[9])
1
9 10
4 8 5 7
2 14
1
2
3
4
5
6 7
8
9
Hepeapify (A,1)
Again set of max heap property.
10
9 7
4 8 5 1
2
1
2
3
4
5
6 7
8
Swap(A[1],A[i])
Swap(A[1],A[8])
2
9 7
4 8 5 1
10
1
2
3
4
5
6 7
8
n=n-1
8-1=7
2
9 7
4 8 5 1
1
2
3
4
5
6
7
Hepeapify (A,1)
Again set of max heap property.
8 7
4 2 5
1
2
3
4
5
6
7
1
9
Swap(A[1],A[i])
Swap(A[1],A[7])
8 7
4 2 5
1
2
3
5
6
7
9
1
4
n=n-1
7-1=6
8 7
4 2
1
2
3
5
6
1
4
5
Hepeapify (A,1)
Again set of max heap property.
4 7
1 2 5
1
2
3
6
8
4
5
Swap(A[1],A[i])
Swap(A[1],A[6])
4 7
1 2 8
1
2
3
6
5
4
5
n=n-1
6-1=5
4 7
1 2
1
2
3
5
4
5
Hepeapify (A,1)
Again set of max heap property.
4 5
1 2
1
2
3
7
4
5
Swap(A[1],A[i])
Swap(A[1],A[5])
4 5
1
7
2
3
4
1
2
n=n-1
5-1=4
4 5
1
2
3
4
2
1
Hepeapify (A,1)
Again set of max heap property.
4 2
1
2
3
4
5
1
Swap(A[1],A[i])
Swap(A[1],[4])
4 22
3
4
1
1
5
n=n-1
4-1=3
4 2
2 3
1
1
Hepeapify (A,1)
Again set of max heap property.
1 2
2 3
4
1
Swap(A[1],A[i])
Swap(A[1],[3])
1 4
2 3
2
1
n=n-1
3-1=2
1
2
2
1
Swap(A[1],A[i])
Swap(A[1],[2])
2
2
1
1
n=n-1
2-1=1
1
1
SORTED LIST.
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
 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
Heap

More Related Content

What's hot (20)

Heap sort
Heap sort Heap sort
Heap sort
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesData Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and Trees
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Heaps
HeapsHeaps
Heaps
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Stacks
StacksStacks
Stacks
 
Queue
QueueQueue
Queue
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Heap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmsHeap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithms
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Heap tree
Heap treeHeap tree
Heap tree
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithm
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Queues
QueuesQueues
Queues
 
Queue
QueueQueue
Queue
 
Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 

Viewers also liked

Viewers also liked (12)

chapter - 6.ppt
chapter - 6.pptchapter - 6.ppt
chapter - 6.ppt
 
Heapsort
HeapsortHeapsort
Heapsort
 
Heapify
HeapifyHeapify
Heapify
 
Heap sort
Heap sortHeap sort
Heap sort
 
lecture 6
lecture 6lecture 6
lecture 6
 
Heapsort
HeapsortHeapsort
Heapsort
 
Heap property
Heap propertyHeap property
Heap property
 
HeapSort
HeapSortHeapSort
HeapSort
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6
 
Heaps
HeapsHeaps
Heaps
 
Heaps
HeapsHeaps
Heaps
 
Heapsort
HeapsortHeapsort
Heapsort
 

Similar to Heap

lecture 5
lecture 5lecture 5
lecture 5sajinsc
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeksJinTaek Seo
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).pptSudhaPatel11
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).pptAmitShou
 
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingAlgorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingTraian Rebedea
 
heap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsheap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsssuser7319f8
 
Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortReetesh Gupta
 

Similar to Heap (20)

lecture 5
lecture 5lecture 5
lecture 5
 
Algorithms - "heap sort"
Algorithms - "heap sort"Algorithms - "heap sort"
Algorithms - "heap sort"
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeks
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
 
Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
 
Heap_Sort1.pptx
Heap_Sort1.pptxHeap_Sort1.pptx
Heap_Sort1.pptx
 
Chapter 10 ds
Chapter 10 dsChapter 10 ds
Chapter 10 ds
 
Heap Tree.pdf
Heap Tree.pdfHeap Tree.pdf
Heap Tree.pdf
 
heapsort_bydinesh
heapsort_bydineshheapsort_bydinesh
heapsort_bydinesh
 
Heapsort quick sort
Heapsort quick sortHeapsort quick sort
Heapsort quick sort
 
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingAlgorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
 
Heapsort
HeapsortHeapsort
Heapsort
 
4 heapsort pq
4 heapsort pq4 heapsort pq
4 heapsort pq
 
heap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsheap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithms
 
Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-Heapsort
 
Heapsort using Heap
Heapsort using HeapHeapsort using Heap
Heapsort using Heap
 
C07.heaps
C07.heapsC07.heaps
C07.heaps
 

More from District Administration (16)

Real time Database
Real time DatabaseReal time Database
Real time Database
 
Presentation on bipolar encoding
Presentation on bipolar encodingPresentation on bipolar encoding
Presentation on bipolar encoding
 
Transactional workflow
Transactional workflowTransactional workflow
Transactional workflow
 
Temporal database
Temporal databaseTemporal database
Temporal database
 
Multimedia Database
Multimedia DatabaseMultimedia Database
Multimedia Database
 
Spatial Database
Spatial DatabaseSpatial Database
Spatial Database
 
Presentations on web database
Presentations on web databasePresentations on web database
Presentations on web database
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
 
Presentation on control access protocol
Presentation on control access protocolPresentation on control access protocol
Presentation on control access protocol
 
Transaction Processing monitor
Transaction Processing monitorTransaction Processing monitor
Transaction Processing monitor
 
Graphical database
Graphical databaseGraphical database
Graphical database
 
Graph database
Graph database Graph database
Graph database
 
Distributed information system
Distributed information systemDistributed information system
Distributed information system
 
Data mining
Data mining Data mining
Data mining
 
Adbms and mmdbms
Adbms and mmdbmsAdbms and mmdbms
Adbms and mmdbms
 
Active and main memory database
Active and main memory databaseActive and main memory database
Active and main memory database
 

Heap