SlideShare a Scribd company logo
CS 321. Algorithm Analysis & Design
Lecture 4
Heap Sort
4 9 2 1 8 5 6
4 9 2 1 8 5 6
4 9 2
1
8 5 6
4 9 2
1
8 5 6
4 9
21
8 5 6
4 9
21
8 5 6
4
9
21
8 5 6
4
9
21
8 5 6
4
9
21
8
5
6
4
9
21
8
5
6
4
9
21
8
5 6
4
9
21
8
5 6
4
9
21 85 6
4
9
21 85 6
4 921 85 6
4 921 85 6
Can we speed up the find-min operations?
4
9
21
8
5
6
Organize the elements so that finding minimum is “easy”.
4
9
21
8
5
6
Organize the elements so that deleting the minimum
element is “easy”.
Store a set of n distinct numbers.
Inserting a new element and deleting the minimum
element can be done in time proportional to (log n).
GOAL
All jumbled up
4 9 2 1 8
All jumbled up
4 9 2 1 8 3
All jumbled up
4 9 2 1 8 3 6
All jumbled up
4 9 2 1 8 3
Insert: O(1)
6
All jumbled up
4 9 2 1 8 3
Insert: O(1) Delete-Min: O(n)
6
All sorted
9 8 6 3 1
All sorted
9 8 6 3
All sorted
9 8 6 3
Delete-Min: O(1)
All sorted
9 8 6 3
Delete-Min: O(1)
4
All sorted
9 8 6 3
Delete-Min: O(1)
47
All sorted
9 8 6 3
Insert: O(n) Delete-Min: O(1)
47
In-between?
Invariant: The first element of
the array is the minimum.
In-between?
Invariant: The first element of
the array is the minimum.
In-between?
Invariant: The first element of
the array is the minimum.
In-between?
Invariant: The first element of
the array is the minimum.
In-between?
Invariant: The first element of
the array is the minimum.
In-between?
Invariant: The first element of
the array is the minimum.
In-between?
Invariant: The first element of
the array is the minimum.
In-between?
Invariant: The first element of
the array is the minimum.
4
9
21
8
5
6
6
1
59
24
8
x is a child of y: x is larger than y.
6
1
59
24
8
6
1
59
24
8
6
1
59
24
8
61 5924 8
61 5924 8
6
1
59
24
8
61 5924 8
6
1
59
24
8
1
2 3
4 5 6 7
1 2 3 4 5 6 7
61 5924 8
6
1
59
24
8
1
2 3
4 5 6 7
1 2 3 4 5 6 7
left-child(i) = 2i
61 5924 8
6
1
4
1
2
4
1 2 3 4 5 6 7
61 5924 8
6
1
4
1
2
4
1 2 3 4 5 6 7
left-child(i) = 2i
left-child(i) = 2i
left-child(i) = 2i
i
2i
left-child(i) = 2i
i+k
2i + 2k
i
2i
6
1
59
24
8
Delete Min
6 1
5
9
24
8
Delete Min
6 1
5
9
24
8
Delete Min
6 1
5
9
24
8
Delete Min
6 1
5
9
24
8
Delete Min
6 1
5
9
24
8
Delete Min
6 1
5
9
2
4
8
Delete Min
6 1
2
9
54
8
Delete Min
6
2
9
54
8
Insert
6
3
2
9
54
8
Insert
6 3
2
9
54
8
Insert
6 3
2
9
54
8
Insert
6
3
2
9 5
4
8
Insert
6
3
2
9 5
4
8
Insert
Heapify

More Related Content

PPT
PDF
LEC 8-DS ALGO(heaps).pdf
PPTX
Kruskal & Prim's Algorithm
PPT
PPT
PPTX
sorting algorithm graphical method
PPT
Minimum Spanning Tree
PDF
one main advantage of bubble sort as compared to others
LEC 8-DS ALGO(heaps).pdf
Kruskal & Prim's Algorithm
sorting algorithm graphical method
Minimum Spanning Tree
one main advantage of bubble sort as compared to others

What's hot (20)

PDF
Sorting
PPT
2.4 mst prim’s algorithm
PPTX
A presentation on prim's and kruskal's algorithm
PDF
05 - 18 Jan - The Sorting Wrap-Up
PPTX
Sorting ppt
PDF
PPTX
Radix sorting
PDF
Work-stealing Tree Data Structure
PDF
Smalltalk
PPT
Searching algorithms
PPTX
Merge sort
PPT
Stacks overview with its applications
PPTX
Sorting2
PDF
Ctrie Data Structure
PPTX
Sign Sign 2
Sorting
2.4 mst prim’s algorithm
A presentation on prim's and kruskal's algorithm
05 - 18 Jan - The Sorting Wrap-Up
Sorting ppt
Radix sorting
Work-stealing Tree Data Structure
Smalltalk
Searching algorithms
Merge sort
Stacks overview with its applications
Sorting2
Ctrie Data Structure
Sign Sign 2
Ad

Similar to 04 - 15 Jan - Heap Sort (20)

PDF
5.1 Priority Queues.pdf 5.1 Priority Queues.pdf5.1 Priority Queues.pdf5.1 Pri...
PPT
Advanced s and s algorithm.ppt
PDF
Chapter 8 advanced sorting and hashing for print
PPTX
Sorting in data structures and algorithms , it has all the necessary points t...
PPTX
sorting.pptx
PPTX
Algorithim lec1.pptx
PPTX
Chapter #4 (Bubble Sort jfdkjffdfdf).pptx
PPTX
L16-L19-Searching&Sorting&string123.pptx
PDF
Sorting and Hashing Algorithm full pdfs.
PPTX
Different Searching and Sorting Methods.pptx
PDF
A practical Introduction to Machine(s) Learning
PDF
Deep Learning and Design Thinking
PPTX
Lecture_5_Stack.pptx
PPT
03_sorting123456789454545454545444543.ppt
PPT
03_sorting and it's types with example .ppt
PPT
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
PPT
Chapter 12 - Heaps.ppt
PDF
Basics in algorithms and data structure
PPTX
Daa final
PDF
01 analysis-of-algorithms
5.1 Priority Queues.pdf 5.1 Priority Queues.pdf5.1 Priority Queues.pdf5.1 Pri...
Advanced s and s algorithm.ppt
Chapter 8 advanced sorting and hashing for print
Sorting in data structures and algorithms , it has all the necessary points t...
sorting.pptx
Algorithim lec1.pptx
Chapter #4 (Bubble Sort jfdkjffdfdf).pptx
L16-L19-Searching&Sorting&string123.pptx
Sorting and Hashing Algorithm full pdfs.
Different Searching and Sorting Methods.pptx
A practical Introduction to Machine(s) Learning
Deep Learning and Design Thinking
Lecture_5_Stack.pptx
03_sorting123456789454545454545444543.ppt
03_sorting and it's types with example .ppt
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
Chapter 12 - Heaps.ppt
Basics in algorithms and data structure
Daa final
01 analysis-of-algorithms
Ad

More from Neeldhara Misra (15)

PDF
Erdos Posa Theorem
PDF
15 - 12 Feb - Stable Matchings
PDF
07- 22 Jan - Divide and Conquer
PDF
10 - 29 Jan - Recursion Part 2
PDF
02 - 04 Jan - Sorting (Continued)
PDF
04 - 15 Jan - Heap Sort
PDF
08 - 25 Jan - Divide and Conquer
PDF
06 - 20 Jan - Divide and Conquer
PDF
13 - 06 Feb - Dynamic Programming
PDF
00 - 30 Dec - Introduction
PDF
01 - 01 January - Sorting
PDF
14 - 08 Feb - Dynamic Programming
PDF
11 - 03 Feb - From Recursion to Dynamic Programming
PDF
09 - 27 Jan - Recursion Part 1
PDF
12 - 05 Feb - Dynamic Programming
Erdos Posa Theorem
15 - 12 Feb - Stable Matchings
07- 22 Jan - Divide and Conquer
10 - 29 Jan - Recursion Part 2
02 - 04 Jan - Sorting (Continued)
04 - 15 Jan - Heap Sort
08 - 25 Jan - Divide and Conquer
06 - 20 Jan - Divide and Conquer
13 - 06 Feb - Dynamic Programming
00 - 30 Dec - Introduction
01 - 01 January - Sorting
14 - 08 Feb - Dynamic Programming
11 - 03 Feb - From Recursion to Dynamic Programming
09 - 27 Jan - Recursion Part 1
12 - 05 Feb - Dynamic Programming

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Complications of Minimal Access Surgery at WLH
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Anesthesia in Laparoscopic Surgery in India
O7-L3 Supply Chain Operations - ICLT Program
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Cell Types and Its function , kingdom of life
102 student loan defaulters named and shamed – Is someone you know on the list?
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Week 4 Term 3 Study Techniques revisited.pptx
Microbial disease of the cardiovascular and lymphatic systems
Complications of Minimal Access Surgery at WLH
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Module 4: Burden of Disease Tutorial Slides S2 2025
TR - Agricultural Crops Production NC III.pdf
Pre independence Education in Inndia.pdf
Pharma ospi slides which help in ospi learning
human mycosis Human fungal infections are called human mycosis..pptx

04 - 15 Jan - Heap Sort