SlideShare a Scribd company logo
1 of 14
Heap Sort
ZAINAB NAAFEY
#1053
BSCS 4TH MORNING
Heap Sort
“Heap sort is a comparison-based sorting technique based on Binary Heap data structure.
It is similar to the selection sort where we first find the minimum element and place the
minimum element at the beginning. Repeat the same process for the remaining elements.”
Heap sort is an in-place algorithm.
Its typical implementation is not stable, but can be made stable. It is 2-3 times slower than
well-implemented Quick Sort. The reason for slowness is a lack of locality of reference.
Advantages of Heap Sort:
Efficiency – The time required to perform Heap sort increases logarithmically while other
algorithms may grow exponentially slower as the number of items to sort increases. This
sorting algorithm is very efficient.
Memory Usage – Memory usage is minimal because apart from what is necessary to hold
the initial list of items to be sorted, it needs no additional memory space to work.
Simplicity – It is simpler to understand than other equally efficient sorting algorithms
because it does not use advanced computer science concepts such as recursion.
Applications of Heap Sort:
Heap Sort is mainly used in hybrid algorithms .
Sort a nearly sorted array.
The heap sort algorithm has limited uses because Quicksort and Merge sort are better in
practice. Nevertheless, the Heap data structure itself is enormously used.
What is meant by Heapify ?
Heapify is the process of creating a heap data structure from a binary tree represented using an
array. It is used to create Min-Heap or Max-heap. Start from the first index of the non-leaf node
whose index is given by n/2 – 1. Heapify uses recursion.
Min Heap & Max Heap
Min Heap:
where the value of root node is less than or equal to either of its children.
Max Heap:
where the value of root node is greater than or equal to either of its children.
Algorithm for Heapify:
Heapify (array)
Root = array[0]
Largest = largest( array[0] , array [2 * 0 + 1]/ array[2 * 0 + 2])
if(Root != Largest)
Swap(Root, Largest)
Working Of Heap Sort
Array = {4,6,10,9,2}
Corresponding Complete Binary Tree is:
Heap:
4
/ 
6 10
/ 
9 2
The task to build a Max-Heap from above array.
Swap 2 and 6:
4
/ 
2 10
/ 
9 6
Working Of Heap Sort
Again swap 4 & 2:
2
/ 
4 10
/ 
9 6
As, parent is smaller than children, we created MinHeap.
Now, Delete Input to reach final output: (always delete from root)
2
/ 
4 10
/ 
9 6
Working Of Heap Sort
Element “2” will be deleted as it is the root element and we will shift the Last Most Right element “6” to the
beginning.
6
/ 
4 10
/
9
Output Array:{2}
As it is not in MinHeap, first Heapify the binary tree.
Swap 4 and 6:
4
/ 
6 10
/
9
Working Of Heap Sort
Again Element “4” will be deleted as it is the root element and we will shift the Last Most Right
element “9” to the beginning.
9
/ 
6 10
Output Array:{2,4)
As it is not in MinHeap, first Heapify the binary tree.
Swap 6 and 9:
6
/ 
9 10
Working Of Heap Sort
Again Element “6” will be deleted as it is the root element and we will shift the Last Most Right
element “10” to the beginning.
10
/
9
Output Array:{2,4,6)
As it is not in MinHeap, first Heapify the binary tree.
Swap 9 and 10:
9
/
10
Working Of Heap Sort
Repeat the same process. Delete the root element “9” and the Last Most Right element “10” is
the last element so it will be placed into the final array.
Output Array:{2,4,6,9,10).
Time Complexity
Time Complexity = O(n) + nlogn
= O(nlogn)

More Related Content

Similar to Heap Sort 1053.pptx

Array implementation & Construction of Heap
Array implementation & Construction of HeapArray implementation & Construction of Heap
Array implementation & Construction of HeapMeghaj Mallick
 
PriorityqDhruvBaswal.pptx
PriorityqDhruvBaswal.pptxPriorityqDhruvBaswal.pptx
PriorityqDhruvBaswal.pptxssuser17c9c21
 
Heap Data Structure Tutorial
Heap Data Structure Tutorial Heap Data Structure Tutorial
Heap Data Structure Tutorial Simplilearn
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printAbdii Rashid
 
QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)Mehedi Hasan
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort Amit Kundu
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfArjunSingh81957
 
SORTING techniques.pptx
SORTING techniques.pptxSORTING techniques.pptx
SORTING techniques.pptxDr.Shweta
 
Heap Hand note
Heap Hand noteHeap Hand note
Heap Hand noteAbdur Rouf
 

Similar to Heap Sort 1053.pptx (20)

HEAP SORT .pptx
HEAP SORT .pptxHEAP SORT .pptx
HEAP SORT .pptx
 
Heap Tree.pdf
Heap Tree.pdfHeap Tree.pdf
Heap Tree.pdf
 
Heaps
HeapsHeaps
Heaps
 
Priority queue
Priority queuePriority queue
Priority queue
 
Array implementation & Construction of Heap
Array implementation & Construction of HeapArray implementation & Construction of Heap
Array implementation & Construction of Heap
 
Heapify algorithm
Heapify algorithmHeapify algorithm
Heapify algorithm
 
Heapsort
HeapsortHeapsort
Heapsort
 
Lec23
Lec23Lec23
Lec23
 
PriorityqDhruvBaswal.pptx
PriorityqDhruvBaswal.pptxPriorityqDhruvBaswal.pptx
PriorityqDhruvBaswal.pptx
 
Heap Data Structure Tutorial
Heap Data Structure Tutorial Heap Data Structure Tutorial
Heap Data Structure Tutorial
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
Heap sort
Heap sortHeap sort
Heap sort
 
QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)QUEUE in data-structure (classification, working procedure, Applications)
QUEUE in data-structure (classification, working procedure, Applications)
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
 
SORTING techniques.pptx
SORTING techniques.pptxSORTING techniques.pptx
SORTING techniques.pptx
 
Ch15 Heap
Ch15 HeapCh15 Heap
Ch15 Heap
 
Heap Hand note
Heap Hand noteHeap Hand note
Heap Hand note
 

Recently uploaded

Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 

Recently uploaded (20)

Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 

Heap Sort 1053.pptx

  • 2. Heap Sort “Heap sort is a comparison-based sorting technique based on Binary Heap data structure. It is similar to the selection sort where we first find the minimum element and place the minimum element at the beginning. Repeat the same process for the remaining elements.” Heap sort is an in-place algorithm. Its typical implementation is not stable, but can be made stable. It is 2-3 times slower than well-implemented Quick Sort. The reason for slowness is a lack of locality of reference.
  • 3. Advantages of Heap Sort: Efficiency – The time required to perform Heap sort increases logarithmically while other algorithms may grow exponentially slower as the number of items to sort increases. This sorting algorithm is very efficient. Memory Usage – Memory usage is minimal because apart from what is necessary to hold the initial list of items to be sorted, it needs no additional memory space to work. Simplicity – It is simpler to understand than other equally efficient sorting algorithms because it does not use advanced computer science concepts such as recursion.
  • 4. Applications of Heap Sort: Heap Sort is mainly used in hybrid algorithms . Sort a nearly sorted array. The heap sort algorithm has limited uses because Quicksort and Merge sort are better in practice. Nevertheless, the Heap data structure itself is enormously used.
  • 5. What is meant by Heapify ? Heapify is the process of creating a heap data structure from a binary tree represented using an array. It is used to create Min-Heap or Max-heap. Start from the first index of the non-leaf node whose index is given by n/2 – 1. Heapify uses recursion.
  • 6. Min Heap & Max Heap Min Heap: where the value of root node is less than or equal to either of its children. Max Heap: where the value of root node is greater than or equal to either of its children.
  • 7. Algorithm for Heapify: Heapify (array) Root = array[0] Largest = largest( array[0] , array [2 * 0 + 1]/ array[2 * 0 + 2]) if(Root != Largest) Swap(Root, Largest)
  • 8. Working Of Heap Sort Array = {4,6,10,9,2} Corresponding Complete Binary Tree is: Heap: 4 / 6 10 / 9 2 The task to build a Max-Heap from above array. Swap 2 and 6: 4 / 2 10 / 9 6
  • 9. Working Of Heap Sort Again swap 4 & 2: 2 / 4 10 / 9 6 As, parent is smaller than children, we created MinHeap. Now, Delete Input to reach final output: (always delete from root) 2 / 4 10 / 9 6
  • 10. Working Of Heap Sort Element “2” will be deleted as it is the root element and we will shift the Last Most Right element “6” to the beginning. 6 / 4 10 / 9 Output Array:{2} As it is not in MinHeap, first Heapify the binary tree. Swap 4 and 6: 4 / 6 10 / 9
  • 11. Working Of Heap Sort Again Element “4” will be deleted as it is the root element and we will shift the Last Most Right element “9” to the beginning. 9 / 6 10 Output Array:{2,4) As it is not in MinHeap, first Heapify the binary tree. Swap 6 and 9: 6 / 9 10
  • 12. Working Of Heap Sort Again Element “6” will be deleted as it is the root element and we will shift the Last Most Right element “10” to the beginning. 10 / 9 Output Array:{2,4,6) As it is not in MinHeap, first Heapify the binary tree. Swap 9 and 10: 9 / 10
  • 13. Working Of Heap Sort Repeat the same process. Delete the root element “9” and the Last Most Right element “10” is the last element so it will be placed into the final array. Output Array:{2,4,6,9,10).
  • 14. Time Complexity Time Complexity = O(n) + nlogn = O(nlogn)