Heaps and Heap Sort: Data Structures and Time Complexity Analysis, Lecture notes of Data Structures and Algorithms

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

2025/2026

Available from 04/29/2026

sllmnhx
sllmnhx 🇵🇰

44 documents

1 / 59

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Heap and Heap Sort
Course: Data Structure and Algorithms
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b

Partial preview of the text

Download Heaps and Heap Sort: Data Structures and Time Complexity Analysis and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Heap and Heap Sort

Course: Data Structure and Algorithms

Heap

A heap is a data structure that stores a

collection of objects (with keys), and has the

following properties:

• Complete Binary tree

• Heap Order

It is implemented as an array where each node

in the tree corresponds to an element of the

array.

Heap

• The root of the tree A[1] and given index i of

a node, the indices of its parent, left child

and right child can be computed

PARENT ( i)

return floor( i/2)

LEFT ( i)

return 2 i

RIGHT ( i)

return 2 i + 1

Heap order property

• For every node v, other than the root, the key

stored in v is greater or equal (smaller or equal

for max heap) than the key stored in the

parent of v.

• In this case the maximum value is stored in

the root

Max Heap Example

Array A

Min heap example

Array A

Insert 17

swap

Percolate up to maintain the

heap property

Deletion

• Delete max

• Copy the last number to the root ( overwrite the maximum

element stored there ).

• Restore the max heap property by percolate down.

• Delete min

• Copy the last number to the root ( overwrite the minimum

element stored there ).

• Restore the min heap property by percolate down.

Procedures on Heap

• Heapify

• Build Heap

• Heap Sort

Heapify

  • (^) Heapify picks the largest child key and compare it to

the parent key. If parent key is larger than heapify

quits, otherwise it swaps the parent key with the

largest child key. So that the parent is now becomes

larger than its children.

Heapify(A, i)

lleft(i)

rright(i)

if l <= heapsize[A] and A[l] > A[i]

then largestl

else largesti

if r <= heapsize[A] and A[r] > A[largest]

then largestr

if largest != i

then swap A[i]  A[largest]

Heapify(A, largest)

Heap Sort Algorithm

• The heap sort algorithm starts by using procedure

BUILD-HEAP to build a heap on the input array A[..

n]. Since the maximum element of the array stored at

the root A[1], it can be put into its correct final

position by exchanging it with A[ n] (the last element

in A). If we now discard node n from the heap than the

remaining elements can be made into heap. Note that

the new element at the root may violate the heap

property. All that is needed to restore the heap

property.

Heapsort(A)

Buildheap(A)

for i  length[A] //down to 2

do swap A[1]  A[i]

heapsize[A]  heapsize[A] - 1

Heapify(A, 1)

Example: Convert the following array to a heap

Picture the array as a complete binary tree:

Heap Sort

• The heapsort algorithm consists of two phases:

  • build a heap from an arbitrary array
  • use the heap to sort the data

• To sort the elements in the decreasing order, use a min heap

• To sort the elements in the increasing order, use a max heap

Example of Heap Sort

Array A

Sorted:

Take out biggest

Move the last element

to the root