



















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This document covers the mechanics of the heap data structure and its application in the Heap Sort algorithm. It defines a heap as a complete binary tree that follows the heap-order property, where the root holds the maximum value in a Max Heap or the minimum in a Min Heap. You will find a technical breakdown of how these trees are efficiently represented as arrays using index calculations for parents and children.The guide includes a detailed time complexity analysis for building a heap and performing the sort. It explains that while building a heap takes O(n) time, the total sorting process requires O(n log n) due to the repeated "reheapify" steps. This resource is a factual reference for students of Data Structures and Algorithms looking to understand balanced tree operations and efficient sorting logic.
Typology: Lecture notes
1 / 59
This page cannot be seen from the preview
Don't miss anything!




















































Array A
Array A
Insert 17
swap
Percolate up to maintain the
heap property
element stored there ).
element stored there ).
Heapify
Heapify(A, i)
l left(i)
r right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest l
else largest i
if r <= heapsize[A] and A[r] > A[largest]
then largest r
if largest != i
then swap A[i] A[largest]
Heapify(A, largest)
Example: Convert the following array to a heap
Picture the array as a complete binary tree:
Array A
Sorted:
Take out biggest
Move the last element
to the root