SlideShare a Scribd company logo
MUHAMMAD MUSADDIQ KHAN
ASSIGNMENT
SUBMITTED
BY
TO
SIR MUHAMMAD BILLAL
HEAP SORT
‫الرحیم‬ ‫الرحمن‬ ‫ہللا‬ ‫بسم‬
Def : A heap is a nearly complete binary tree
TYPES:
•Max-heaps (largest element at root), have the max-
heap
property:
– for all nodes i, excluding the root: A[PARENT(i)] ≥
A[i]
•Min-heaps (smallest element at root), have the
min-heap
property:
– for all nodes i, excluding the root: A[PARENT(i)] ≤
A[i]
– Structural property: all levels are full, except
possibly the last one, which is filled from left to right
– Order (heap) property: for any node x
Parent(x) ≥ x
A heap is a binary tree that is filled in order
From the heap property, it
follows that:
“The root is the maximum
element
of the heap!”
8
7
610
10
8
• Goal:
– Sort an array using heap representations
• Idea:
1– Build a max-heap from the array
2– Swap the root (the maximum element) with
the last element in the array
3– “Discard” this last node by decreasing the
heap size
4– Call MAX-HEAPIFY on the new root
5– Repeat this process until only one node
remains
Heapsort
Insertion.
Heapify.
Deletion.
Operations on Heaps
• New nodes are always inserted at the bottom
level
(left to right)
• Nodes are removed from the bottom level
(right to left)
Rule of Adding/Deleting Nodes
Array Representation of Heaps
• A heap can be stored as an
array A.
– Root of tree is A[1]
– Left child of A[i] = A[2i]
– Right child of A[i] = A[2i + 1]
– Parent of A[i] = A[ i/2 ]
The elements in the sub array
A[( n/2 +1) ….. n] are leaves
• Given a node that does not have the heap property,
you can give it the heap property by exchanging its
value with the value of the smaller child
• This is sometimes called shifting up
• Notice that the child may have lost the heap property
Shift Up
8
106
6
108
8
• Each time we add a node, we may destroy the
heap property of its parent node
• To fix this, we sift up
• We repeat the sifting up process, moving up in
the tree, until either
– We reach nodes whose values don’t need
to be swapped (because the parent is still
larger than both children), or
– We reach the root
Problem during Constructing a heap
8
9
610
96108
0 1 2 3 4
Make a Tree from Array
8
9
610
Make Max Heap from Tree
10
8
8
9
610
Make Min Heap from Tree
6
8 10
6
9
108
6
9
108
5
After inserting 5
Heapify property of
Min heap may be is
destroy, again
make it min heap
Insert following number in tree: 5
We can perform Major operations on
heaps:
 Heapify which runs in O(log n) time.
 Build-heap which returns in linear time.
 Heap sort, which runs in O(n log n) time.
Time Complexity
ANY
???

More Related Content

PPTX
Heap Sort in Design and Analysis of algorithms
PDF
Heap and heapsort
PPTX
Binomial heap presentation
PPTX
Heap sort
PPT
Lec 17 heap data structure
PPTX
HEAP SORT .pptx
PDF
Fibonacci Heap
PPTX
All pair shortest path
Heap Sort in Design and Analysis of algorithms
Heap and heapsort
Binomial heap presentation
Heap sort
Lec 17 heap data structure
HEAP SORT .pptx
Fibonacci Heap
All pair shortest path

What's hot (20)

PPTX
Data Structures (CS8391)
PPTX
Quick sort
PPT
Master method theorem
PPT
Heap Sort (project).ppt
PDF
Divide and Conquer
PPTX
PPT
3.9 external sorting
PDF
Expression trees
PPTX
Hashing in datastructure
PPTX
strassen matrix multiplication algorithm
PPTX
Complexity analysis in Algorithms
PDF
Master theorem
PPT
Quick Sort
PPTX
Data structures
PPT
Stacks
PPT
PPSX
Data Structure (Queue)
PPTX
Hash table
PPTX
Linear and Binary search
PPTX
Linked list
Data Structures (CS8391)
Quick sort
Master method theorem
Heap Sort (project).ppt
Divide and Conquer
3.9 external sorting
Expression trees
Hashing in datastructure
strassen matrix multiplication algorithm
Complexity analysis in Algorithms
Master theorem
Quick Sort
Data structures
Stacks
Data Structure (Queue)
Hash table
Linear and Binary search
Linked list
Ad

Similar to Heap Sort Algorithm (20)

PPT
Heapsort ppt
PPTX
Algorithms - "heap sort"
PPT
Analysis of Algorithms-Heapsort
PPT
Unit III Heaps.ppt
PDF
Heap Hand note
PPTX
05 heap 20161110_jintaeks
PPTX
heap Sort Algorithm
PPT
CSE680-06HeapSort.ppt
PDF
Heapsort quick sort
PPTX
Heap sort
PPTX
Binary Heap Tree, Data Structure
PPT
thisisheapsortpptfilewhichyoucanuseanywhereanytim
PPT
Cis435 week05
PPT
3.7 heap sort
PDF
HeapSort
PPTX
Heap_data_structures_in_data_steruc.pptx
PPT
21. Heap_new.ppt
PPTX
Data structures and algorithms lab10
PPTX
Lecture 07 - HeapSort.pptx
Heapsort ppt
Algorithms - "heap sort"
Analysis of Algorithms-Heapsort
Unit III Heaps.ppt
Heap Hand note
05 heap 20161110_jintaeks
heap Sort Algorithm
CSE680-06HeapSort.ppt
Heapsort quick sort
Heap sort
Binary Heap Tree, Data Structure
thisisheapsortpptfilewhichyoucanuseanywhereanytim
Cis435 week05
3.7 heap sort
HeapSort
Heap_data_structures_in_data_steruc.pptx
21. Heap_new.ppt
Data structures and algorithms lab10
Lecture 07 - HeapSort.pptx
Ad

Recently uploaded (20)

PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
RMMM.pdf make it easy to upload and study
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Cell Structure & Organelles in detailed.
PDF
Classroom Observation Tools for Teachers
PDF
01-Introduction-to-Information-Management.pdf
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Updated Idioms and Phrasal Verbs in English subject
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PDF
A systematic review of self-coping strategies used by university students to ...
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Complications of Minimal Access Surgery at WLH
Weekly quiz Compilation Jan -July 25.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
RMMM.pdf make it easy to upload and study
Chinmaya Tiranga quiz Grand Finale.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Cell Structure & Organelles in detailed.
Classroom Observation Tools for Teachers
01-Introduction-to-Information-Management.pdf
History, Philosophy and sociology of education (1).pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Updated Idioms and Phrasal Verbs in English subject
LDMMIA Reiki Yoga Finals Review Spring Summer
Paper A Mock Exam 9_ Attempt review.pdf.
Yogi Goddess Pres Conference Studio Updates
2.FourierTransform-ShortQuestionswithAnswers.pdf
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
A systematic review of self-coping strategies used by university students to ...

Heap Sort Algorithm

  • 3. Def : A heap is a nearly complete binary tree TYPES: •Max-heaps (largest element at root), have the max- heap property: – for all nodes i, excluding the root: A[PARENT(i)] ≥ A[i] •Min-heaps (smallest element at root), have the min-heap property: – for all nodes i, excluding the root: A[PARENT(i)] ≤ A[i]
  • 4. – Structural property: all levels are full, except possibly the last one, which is filled from left to right – Order (heap) property: for any node x Parent(x) ≥ x A heap is a binary tree that is filled in order From the heap property, it follows that: “The root is the maximum element of the heap!” 8 7 610 10 8
  • 5. • Goal: – Sort an array using heap representations • Idea: 1– Build a max-heap from the array 2– Swap the root (the maximum element) with the last element in the array 3– “Discard” this last node by decreasing the heap size 4– Call MAX-HEAPIFY on the new root 5– Repeat this process until only one node remains Heapsort
  • 7. • New nodes are always inserted at the bottom level (left to right) • Nodes are removed from the bottom level (right to left) Rule of Adding/Deleting Nodes
  • 8. Array Representation of Heaps • A heap can be stored as an array A. – Root of tree is A[1] – Left child of A[i] = A[2i] – Right child of A[i] = A[2i + 1] – Parent of A[i] = A[ i/2 ] The elements in the sub array A[( n/2 +1) ….. n] are leaves
  • 9. • Given a node that does not have the heap property, you can give it the heap property by exchanging its value with the value of the smaller child • This is sometimes called shifting up • Notice that the child may have lost the heap property Shift Up 8 106 6 108 8
  • 10. • Each time we add a node, we may destroy the heap property of its parent node • To fix this, we sift up • We repeat the sifting up process, moving up in the tree, until either – We reach nodes whose values don’t need to be swapped (because the parent is still larger than both children), or – We reach the root Problem during Constructing a heap
  • 11. 8 9 610 96108 0 1 2 3 4 Make a Tree from Array
  • 12. 8 9 610 Make Max Heap from Tree 10 8
  • 13. 8 9 610 Make Min Heap from Tree 6 8 10
  • 14. 6 9 108 6 9 108 5 After inserting 5 Heapify property of Min heap may be is destroy, again make it min heap Insert following number in tree: 5
  • 15. We can perform Major operations on heaps:  Heapify which runs in O(log n) time.  Build-heap which returns in linear time.  Heap sort, which runs in O(n log n) time. Time Complexity