Understanding
Heap creation
Dr. Gopika S
Assistant Professor
Department of Computer Science
Kristu Jayanti College
Heap & Heap Sort – Introduction
• Many sorting methods exists to arrange elements in ascending or
descending order like bubble sort , merge sort, selection sort etc
• A heap sort is a sorting method which uses the concept of heap.
• What is a heap ? A heap is a variant of binary tree
• Types of Heap
 Min heap
Max heap
Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru
Construction of Max Heap
Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru
Example data set = [ 23,34,44,17,21,18,6]
23
34
34
23
Heap property is violated - Not a
heap Now its a heap
Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru
23
34
Example data set = [ 23,34,44,17,21,18,6]
44
44
34 23
Example data set = [ 23,34,44,17,21,18,6]
Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru
44
34 23
17 21 18 6
44 34 23 17 21 18 6
HEAP IS CONSTRUCTED
Array representation of heap
HEAP SORT - STEPS
1. Create the heap using the available elements
2. Remove the root element of the heap and insert it as the last element of
the array
3. Repeat the above steps
Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru
HEAP SORT
Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti
College, Bengaluru
44
34 23
17 21 18 6
44 34 23 17 21 18 6
HEAP IS CONSTRUCTED
Array representation of heap
HEAP SORT
Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru
6
34 23
17 21 18 44
6 34 23 17 21 18 44
Array representation of heap
HEAP SORT
Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru
6
34 23
17 21 18
34
21 23
17 6 18
Heap Sort ( Continued…)
34 21 23 17 6 18
Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru
Array Representation
18 21 23 17 6 34
34
21 23
17 6 18
Heap sort (Continued…)
Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru
18
21 23
17 6
23
21 18
17 6
NOT A HEAP
NOW, THIS IS A HEAP
Heap Sort ( Continued…)
Continue the steps for heap sort
1. Create the heap using the available elements
2. Remove the root element of the heap and insert it
as the last element of the array
Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru
23
21 18
17 6
23 34 44
Sorted elements in the final array are
Heap Sort ( Continued…)
Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru
6
21 18
17
21
17 18
6
Final Array After sorting
Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru
6 17 18 21 23 34 44
Sorted elements in the final array are

Heap creation from elements - Step by step Approach

  • 1.
    Understanding Heap creation Dr. GopikaS Assistant Professor Department of Computer Science Kristu Jayanti College
  • 2.
    Heap & HeapSort – Introduction • Many sorting methods exists to arrange elements in ascending or descending order like bubble sort , merge sort, selection sort etc • A heap sort is a sorting method which uses the concept of heap. • What is a heap ? A heap is a variant of binary tree • Types of Heap  Min heap Max heap Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru
  • 3.
    Construction of MaxHeap Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru Example data set = [ 23,34,44,17,21,18,6] 23 34 34 23 Heap property is violated - Not a heap Now its a heap
  • 4.
    Understanding Heap sortMs. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru 23 34 Example data set = [ 23,34,44,17,21,18,6] 44 44 34 23
  • 5.
    Example data set= [ 23,34,44,17,21,18,6] Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru 44 34 23 17 21 18 6 44 34 23 17 21 18 6 HEAP IS CONSTRUCTED Array representation of heap
  • 6.
    HEAP SORT -STEPS 1. Create the heap using the available elements 2. Remove the root element of the heap and insert it as the last element of the array 3. Repeat the above steps Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru
  • 7.
    HEAP SORT Understanding Heapsort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru 44 34 23 17 21 18 6 44 34 23 17 21 18 6 HEAP IS CONSTRUCTED Array representation of heap
  • 8.
    HEAP SORT Understanding Heapsort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru 6 34 23 17 21 18 44 6 34 23 17 21 18 44 Array representation of heap
  • 9.
    HEAP SORT Understanding Heapsort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru 6 34 23 17 21 18 34 21 23 17 6 18
  • 10.
    Heap Sort (Continued…) 34 21 23 17 6 18 Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru Array Representation 18 21 23 17 6 34 34 21 23 17 6 18
  • 11.
    Heap sort (Continued…) UnderstandingHeap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru 18 21 23 17 6 23 21 18 17 6 NOT A HEAP NOW, THIS IS A HEAP
  • 12.
    Heap Sort (Continued…) Continue the steps for heap sort 1. Create the heap using the available elements 2. Remove the root element of the heap and insert it as the last element of the array Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru 23 21 18 17 6 23 34 44 Sorted elements in the final array are
  • 13.
    Heap Sort (Continued…) Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru 6 21 18 17 21 17 18 6
  • 14.
    Final Array Aftersorting Understanding Heap sort Ms. Gopika S , Department of Computer Science, Kristu Jayanti College, Bengaluru 6 17 18 21 23 34 44 Sorted elements in the final array are