SlideShare a Scribd company logo
3
Most read
11
Most read
17
Most read
DESIGN AND ANALYSIS OF
ALGORITHM
GC. Women University, FSD
Topic: Heap Sort
Submitted To: Ms. Sadia Sahar
Submitted by:Samaira Akram (15-175)
ANALYSIS OF
ALGORITHMS
Heap Sort Algorithm
HEAP SORT
• Sorting technique based upon a “Binary Heap Data
Structure”.
• Also called “In-place algorithm”.
• One of the efficient sorting methods.
• No quadratic worst-case running time.
• Involves building a heap data structure from given array.
• Similar to Selection sort.
• Finds minimum/maximum element and place in correct place.
WHAT IS A HEAP?
• An untidy collection of objects placed haphazardly on top of
each other. (General meaning)
• Special binary tree-based data structure.
Satisfies the special heap properties:
1. Shape Property: Complete binary tree (all levels of tree
are fully filled).
2. Heap Property: Nodes are either greater than or equal to
or less than or equal to each of it’s children.
BINARY TREE
• Each node having at most 2 nodes/leaves/children.
• Children can be left child and right child.
• Binary tree consists of:
• Root node
• Parent node(s)
• Child node(s)
• Depth/Level of node: Number of edges from root to node.
• Height of a node: Number of edges from the node to deepest
leaf.
• Height of tree: Height of the root.
PICTORIAL REPRESENTATION
1
2
4 5
3
6
Root Node
Parent Nodes
Children
Height of node
(2)=1
Height of tree =2
Level/Depth of
node (6) =2
TYPES OF BINARY TREE
• Full Binary Tree
• Each node has exactly zero or 2 children.
• Complete Binary Tree
• All leaves are on same level & all internal nodes have degree 2.
• In-complete Binary Tree
• Either leaf is missing ( not having exactly degree 2).
TYPES OF BINARY TREE (PIC.)
1
2
4 5
3
1
2
4 5
3
6 7
1
2
4 5
3
6
Full Binary Tree Complete Binary Tree
In-complete Binary Tree
Missing.. 
1
2 3
ARRAY REPRESENTATION OF
HEAPS
• 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]
 Heap-size [A] ≤ length [A]
• The elements in the subarray are leaves.
16 14 10 8 7 9
1 2 3 4 5
6
4 5 6
TYPES OF HEAP
1. Max-Heap (Largest element at root) Common!
 Parent nodes are greater than their child nodes (usually used to
sort a list in descending order).
2. Min-Heap (Smallest element at root)
 Parent nodes are smaller than their child nodes (usually used to
sort a list in ascending order). Sorted Array!
MIN-HEAP VS. MAX-HEAP
1
3 4
67 17
1
2 3
4 5
6
8
911
8 9
7
20
17 14
811 9
1
2 3
4 5
6
7
16
8 9
7
Min-Heap
As first element is smallest, so to sort a list
in ascending order, we create a Min-heap
from that list & picks the first element and
repeat the process with remaining elements.
Max-Heap
As first element is largest, so used to sort a
list in descending order.
ADDING/DELETING NODES
• New nodes are always inserted at the bottom level (left to right).
• Nodes are removed from the bottom level (right to left).
1
3 4
67 17
1
2 3
4 5
6
8
911
8 9
7
What type
of heap is
it?
ALGORITHM OF HEAP SORT
HeapSort(A)
BuildMaxHeap(A)
for i = length[A] downto 2
do swap A[1] with A[i]
heap-size[A] = heap-size[A] – 1
MaxHeapify(A, 1)
end func
BuildMaxHeap(A)
heap-size[A] = length[A]
for i = |length[A]/2| downto 1
do MaxHeapify(A, i)
end func
ALGORITHM OF HEAP SORT
(CONT.)
MaxHeapify(A, i)
l = left(i)
r = right(i)
if l <= heap-size[A] and A[l] > A[i]
then largest = l
else largest = i
if r <= heap-size[A] and A[r] > A[largest]
then largest = r
if largest != i
then swap A[i] with A[largest]
MaxHeapify(A, largest)
end func
HOW HEAP SORT WORKS?
• Heap sort algorithm inserts all elements (from an unsorted
array) into a heap then swap the first element (maximum) with
the last element (minimum) and reduce the heap size (array
size) by 1, because last element is already at its final location in
the sorted array. Then we heapify the first element because
swapping has broken the Max-heap property. We continue this
process until heap size remains one.
• Hereafter swapping, it may not satisfy the heap property. So we
need to adjust the location of element to maintain heap
property.
This process is called “Heapify”.
CONVERTING ARRAY TO MAX-
HEAP
ANALYSIS OF HEAPIFY
Applying recurrence analysis:
T(n)= a*T(n/b)+f(n)
T(n)= T(2n/3)+1
Applying Master Method:
a=1
b=3/2
f(n)=1
nlog
b
a = nlog
3/2
1=n0=1 :: Case 2 applies since f(n)= nlog
b
a
T(n)=O( nlog
b
a logn)
T(n)= O( logn)
ANALYSIS OF HEAP SORT
Times
HeapSort(A)
BuildMaxHeap(A) 1(n)
for i = length[A] downto 2 n
do swap A[1] with A[i] n-1
heap-size[A] = heap-size[A] – 1 n-1
MaxHeapify(A, 1) n(logn)
end func
ANALYSIS OF HEAP SORT
(CONT.)
Since in Heap Sort we first call Build-Max-Heap function
which takes O(n) time then we call Max-Heapify function n
time, where Max-Heapify function takes O(logn) time so, total
time complexity of heap-sort comes out to be O(n* logn).
Summarizing all this:
Time Complexity of Heap Sort:
Worst Case : O (n* logn)
Average Case : O(n* logn)
Best Case : O(n* logn)
COMPARISON
Heap Sort Quick Sort Merge Sort
Selection method. Partitioning method . Merging method.
It is not stable sort. It is not stable sort. Merge is stable sort.
Doesn’t need
supplementary
memory to work.
Need supplementary
memory to work.
Need supplementary
memory to work.
Example of “In place”
sorting algorithm.
Example of recursive
divide and conquer.
Example of recursive
divide and conquer.
Not copy the array
and store it
elsewhere.
Copy the array and
store it elsewhere.
Copy the array and
store it elsewhere.
Heap Sort Quick Sort Merge Sort
Needs less space. Needs more space. Needs more space.
Example:-
Used in situations
like what's the top 10
numbers from a list
of 1 million numbers.
Example:-
Java uses a version
of quick sort called
as double pivoted
quick sort to sort
primitives.
Example:-
Merge sort works
best on linked lists.
COMPARISON
ADVANTAGES
PROS. OF HEAP SORT
Efficiency: The Heap sort algorithm is very efficient.
Memory usage: The Heap sort algorithm can be implemented as an
in-place sorting algorithm.
Simplicity: The Heap sort algorithm is simpler to understand than
other equally efficient sorting algorithms.
Consistency: The Heap sort algorithm exhibits consistent
performance.
DISADVANTAGES
CONS. OF HEAP SORT
More Storage is required: Heap sort requires more space for sorting.
Not efficient in some cases: Quick sort is much more efficient than
Heap in many cases.
Time consuming: Heap sort make a tree of sorting elements.
Slower: Generally slower than quick and merge sorts.
THE END!


More Related Content

PPTX
Producer consumer problem operating system
PPTX
3.rm the literature review
PPT
Encoder, decoder, multiplexers and demultiplexers
PDF
Algorithms Lecture 4: Sorting Algorithms I
PPTX
heap Sort Algorithm
PPTX
Ppt semi conductor
PPTX
Propositional logic
PDF
Intel 8051 - pin description
Producer consumer problem operating system
3.rm the literature review
Encoder, decoder, multiplexers and demultiplexers
Algorithms Lecture 4: Sorting Algorithms I
heap Sort Algorithm
Ppt semi conductor
Propositional logic
Intel 8051 - pin description

What's hot (20)

PPTX
Turing machine-TOC
PPTX
Semantic net in AI
PPTX
Complexity analysis in Algorithms
PPT
Parsing
PPTX
Graph traversals in Data Structures
PPTX
Breadth First Search & Depth First Search
PPTX
Sorting Algorithms
PPTX
Demand paging
PPTX
Problem reduction AND OR GRAPH & AO* algorithm.ppt
PDF
Binary Search - Design & Analysis of Algorithms
PPT
Backtracking Algorithm.ppt
PPTX
Quick sort
PDF
State Space Search in ai
PPTX
String matching algorithms
PPTX
Asymptotic Notation
PPT
Parallel processing
PPT
DESIGN AND ANALYSIS OF ALGORITHMS
PPTX
Webinar : P, NP, NP-Hard , NP - Complete problems
PPT
Asymptotic notations
Turing machine-TOC
Semantic net in AI
Complexity analysis in Algorithms
Parsing
Graph traversals in Data Structures
Breadth First Search & Depth First Search
Sorting Algorithms
Demand paging
Problem reduction AND OR GRAPH & AO* algorithm.ppt
Binary Search - Design & Analysis of Algorithms
Backtracking Algorithm.ppt
Quick sort
State Space Search in ai
String matching algorithms
Asymptotic Notation
Parallel processing
DESIGN AND ANALYSIS OF ALGORITHMS
Webinar : P, NP, NP-Hard , NP - Complete problems
Asymptotic notations
Ad

Similar to Heap Sort in Design and Analysis of algorithms (20)

PPTX
comparisonbasedsortingalgorithmHeap Sort.pptx
PPT
heap sort in the design anad analysis of algorithms
PPTX
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
PPT
Heap Sort (project).ppt
PPT
Heap Sort (project).pptccccccccccccccccccccccccccccccccccc
PPT
Data Structure Heap Sort Algorithom PPT
PPT
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
PPT
Heap Sort (project).ppt
PPT
Heap Sort (project).ppt
PPT
Heapsort ppt
PPTX
Heap_data_structures_in_data_steruc.pptx
PPTX
Heap Sort 1053.pptx
PPTX
Heapsort using Heap
PPTX
data structures and algorithms Unit 3
PPTX
Heaps & its operation -Max Heap, Min Heap
PPTX
Lecture 3 - Data Structure File Organization
PPT
Unit III Heaps.ppt
PPT
Sorting Seminar Presentation by Ashin Guha Majumder
PPTX
Data structures and algorithms lab10
comparisonbasedsortingalgorithmHeap Sort.pptx
heap sort in the design anad analysis of algorithms
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Heap Sort (project).ppt
Heap Sort (project).pptccccccccccccccccccccccccccccccccccc
Data Structure Heap Sort Algorithom PPT
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (project).ppt
Heap Sort (project).ppt
Heapsort ppt
Heap_data_structures_in_data_steruc.pptx
Heap Sort 1053.pptx
Heapsort using Heap
data structures and algorithms Unit 3
Heaps & its operation -Max Heap, Min Heap
Lecture 3 - Data Structure File Organization
Unit III Heaps.ppt
Sorting Seminar Presentation by Ashin Guha Majumder
Data structures and algorithms lab10
Ad

Recently uploaded (20)

PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Yogi Goddess Pres Conference Studio Updates
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Classroom Observation Tools for Teachers
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
Weekly quiz Compilation Jan -July 25.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
A systematic review of self-coping strategies used by university students to ...
Yogi Goddess Pres Conference Studio Updates
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Classroom Observation Tools for Teachers
202450812 BayCHI UCSC-SV 20250812 v17.pptx
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf

Heap Sort in Design and Analysis of algorithms

  • 1. DESIGN AND ANALYSIS OF ALGORITHM GC. Women University, FSD Topic: Heap Sort Submitted To: Ms. Sadia Sahar Submitted by:Samaira Akram (15-175)
  • 3. HEAP SORT • Sorting technique based upon a “Binary Heap Data Structure”. • Also called “In-place algorithm”. • One of the efficient sorting methods. • No quadratic worst-case running time. • Involves building a heap data structure from given array. • Similar to Selection sort. • Finds minimum/maximum element and place in correct place.
  • 4. WHAT IS A HEAP? • An untidy collection of objects placed haphazardly on top of each other. (General meaning) • Special binary tree-based data structure. Satisfies the special heap properties: 1. Shape Property: Complete binary tree (all levels of tree are fully filled). 2. Heap Property: Nodes are either greater than or equal to or less than or equal to each of it’s children.
  • 5. BINARY TREE • Each node having at most 2 nodes/leaves/children. • Children can be left child and right child. • Binary tree consists of: • Root node • Parent node(s) • Child node(s) • Depth/Level of node: Number of edges from root to node. • Height of a node: Number of edges from the node to deepest leaf. • Height of tree: Height of the root.
  • 6. PICTORIAL REPRESENTATION 1 2 4 5 3 6 Root Node Parent Nodes Children Height of node (2)=1 Height of tree =2 Level/Depth of node (6) =2
  • 7. TYPES OF BINARY TREE • Full Binary Tree • Each node has exactly zero or 2 children. • Complete Binary Tree • All leaves are on same level & all internal nodes have degree 2. • In-complete Binary Tree • Either leaf is missing ( not having exactly degree 2).
  • 8. TYPES OF BINARY TREE (PIC.) 1 2 4 5 3 1 2 4 5 3 6 7 1 2 4 5 3 6 Full Binary Tree Complete Binary Tree In-complete Binary Tree Missing.. 
  • 9. 1 2 3 ARRAY REPRESENTATION OF HEAPS • 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]  Heap-size [A] ≤ length [A] • The elements in the subarray are leaves. 16 14 10 8 7 9 1 2 3 4 5 6 4 5 6
  • 10. TYPES OF HEAP 1. Max-Heap (Largest element at root) Common!  Parent nodes are greater than their child nodes (usually used to sort a list in descending order). 2. Min-Heap (Smallest element at root)  Parent nodes are smaller than their child nodes (usually used to sort a list in ascending order). Sorted Array!
  • 11. MIN-HEAP VS. MAX-HEAP 1 3 4 67 17 1 2 3 4 5 6 8 911 8 9 7 20 17 14 811 9 1 2 3 4 5 6 7 16 8 9 7 Min-Heap As first element is smallest, so to sort a list in ascending order, we create a Min-heap from that list & picks the first element and repeat the process with remaining elements. Max-Heap As first element is largest, so used to sort a list in descending order.
  • 12. ADDING/DELETING NODES • New nodes are always inserted at the bottom level (left to right). • Nodes are removed from the bottom level (right to left). 1 3 4 67 17 1 2 3 4 5 6 8 911 8 9 7 What type of heap is it?
  • 13. ALGORITHM OF HEAP SORT HeapSort(A) BuildMaxHeap(A) for i = length[A] downto 2 do swap A[1] with A[i] heap-size[A] = heap-size[A] – 1 MaxHeapify(A, 1) end func BuildMaxHeap(A) heap-size[A] = length[A] for i = |length[A]/2| downto 1 do MaxHeapify(A, i) end func
  • 14. ALGORITHM OF HEAP SORT (CONT.) MaxHeapify(A, i) l = left(i) r = right(i) if l <= heap-size[A] and A[l] > A[i] then largest = l else largest = i if r <= heap-size[A] and A[r] > A[largest] then largest = r if largest != i then swap A[i] with A[largest] MaxHeapify(A, largest) end func
  • 15. HOW HEAP SORT WORKS? • Heap sort algorithm inserts all elements (from an unsorted array) into a heap then swap the first element (maximum) with the last element (minimum) and reduce the heap size (array size) by 1, because last element is already at its final location in the sorted array. Then we heapify the first element because swapping has broken the Max-heap property. We continue this process until heap size remains one. • Hereafter swapping, it may not satisfy the heap property. So we need to adjust the location of element to maintain heap property. This process is called “Heapify”.
  • 16. CONVERTING ARRAY TO MAX- HEAP
  • 17. ANALYSIS OF HEAPIFY Applying recurrence analysis: T(n)= a*T(n/b)+f(n) T(n)= T(2n/3)+1 Applying Master Method: a=1 b=3/2 f(n)=1 nlog b a = nlog 3/2 1=n0=1 :: Case 2 applies since f(n)= nlog b a T(n)=O( nlog b a logn) T(n)= O( logn)
  • 18. ANALYSIS OF HEAP SORT Times HeapSort(A) BuildMaxHeap(A) 1(n) for i = length[A] downto 2 n do swap A[1] with A[i] n-1 heap-size[A] = heap-size[A] – 1 n-1 MaxHeapify(A, 1) n(logn) end func
  • 19. ANALYSIS OF HEAP SORT (CONT.) Since in Heap Sort we first call Build-Max-Heap function which takes O(n) time then we call Max-Heapify function n time, where Max-Heapify function takes O(logn) time so, total time complexity of heap-sort comes out to be O(n* logn). Summarizing all this: Time Complexity of Heap Sort: Worst Case : O (n* logn) Average Case : O(n* logn) Best Case : O(n* logn)
  • 20. COMPARISON Heap Sort Quick Sort Merge Sort Selection method. Partitioning method . Merging method. It is not stable sort. It is not stable sort. Merge is stable sort. Doesn’t need supplementary memory to work. Need supplementary memory to work. Need supplementary memory to work. Example of “In place” sorting algorithm. Example of recursive divide and conquer. Example of recursive divide and conquer. Not copy the array and store it elsewhere. Copy the array and store it elsewhere. Copy the array and store it elsewhere.
  • 21. Heap Sort Quick Sort Merge Sort Needs less space. Needs more space. Needs more space. Example:- Used in situations like what's the top 10 numbers from a list of 1 million numbers. Example:- Java uses a version of quick sort called as double pivoted quick sort to sort primitives. Example:- Merge sort works best on linked lists. COMPARISON
  • 22. ADVANTAGES PROS. OF HEAP SORT Efficiency: The Heap sort algorithm is very efficient. Memory usage: The Heap sort algorithm can be implemented as an in-place sorting algorithm. Simplicity: The Heap sort algorithm is simpler to understand than other equally efficient sorting algorithms. Consistency: The Heap sort algorithm exhibits consistent performance.
  • 23. DISADVANTAGES CONS. OF HEAP SORT More Storage is required: Heap sort requires more space for sorting. Not efficient in some cases: Quick sort is much more efficient than Heap in many cases. Time consuming: Heap sort make a tree of sorting elements. Slower: Generally slower than quick and merge sorts.