0% found this document useful (0 votes)
20 views12 pages

Heap Sort Lecture2 & 3

Uploaded by

fa22bscs0068
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views12 pages

Heap Sort Lecture2 & 3

Uploaded by

fa22bscs0068
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Design and Analysis of

Algorithms

Lecture: 7 Heap Sort

Engr. Simran Melwani


Heap Sort

• Heap sort is a comparison-based sorting algorithm that works by first building a binary tree-like data
structure called a "heap", and then repeatedly extracting the maximum (or minimum) element from
the heap to build the sorted output.

• This efficient algorithm has wide applications in computer science, from priority queue
implementations to external sorting of large datasets.
Heaps

A heap is a special kind of binary tree that follows two main rules:
1.Shape property: The tree is "complete," which means that every level is fully filled except for possibly the last one,
where only the rightmost spots might be empty. The tree grows from top to bottom and left to right.

2.Heap property: In a max-heap, the value in each parent node is bigger than (or equal to) the values in its child
nodes. This rule ensures that the largest value is always at the top of the tree.
Heaps
In a min-heap, the value of each parent node is smaller than (or equal to) the values of its child
nodes. This means the smallest value is always at the top of the tree.
Maintaining the Heap property

• The first step in building a max-heap is to

ensure that each subtree satisfies the heap

property.

• This is done through a process called

"heapify", where the tree is restructured by

swapping nodes to ensure that each parent

node is greater than or equal to its children.


Building a Heap

This is achieved by starting from the last non-leaf node

and applying the heapify process recursively up the tree,

ensuring that the heap property is maintained

throughout.
Heapsort
Time Complexity

The time complexity of heap sort is O(n log n), where n is the size of Comparison to Other Sorting Algorithms

the input array. This is because the build max-heap step takes O(n) Compared to other popular sorting algorithms like quicksort and

time, and the extract maximum step takes O(log n) time for each of merge sort, heap sort has a slightly higher time complexity but a

the n elements. lower space complexity, making it a good choice for sorting large

datasets with limited memory resources.

Space Complexity

The space complexity of heap sort is O(1), as it is an in-place sorting

algorithm that only requires a constant amount of additional space to

store temporary variables and the heap data structure.


Priority Queue
• Task Scheduling Based On Urgency

• Fix critical bug (Priority 1)

• Deploy to production (Priority 2)

• Write documentation (Priority 3)

• Perform code review (Priority 4)


Heap Maximum

• The heap maximum operation returns the maximum element (the root) of the max-heap without removing it from the heap.
HEAP-EXTRACT-MAX

• The heap extract maximum operation removes the maximum element (the root) from the max-heap and then restructures
the heap to maintain the max-heap property.
Thank you

You might also like