0% found this document useful (0 votes)
8 views

3_HeapSort

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)
8 views

3_HeapSort

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/ 36

Heap Sort

Tree based data structure


Heap Sort

Merge Sort Insertion Sort


O(n log n) (In-Place sorting)

Heap Sort
O(n log n)
In-Place sorting
Heap Sort
• Heap sort is an O(nlogn) comparison based in-place sorting
algorithm.
• Heap sort is not a stable sorting algorithm.
• Heap sort uses a Heap data structure.
• Heap data structure is also used in priority queue.
Heaps
Tree based data structure
Why Heaps?
• There are many data structures which can perform operations such as
search, insert, delete, findMin, findMax etc.
• But every data structure optimizes any one ot few of these
operations.
• Linked list: Insertion/deletion takes O(1), whereas searching takes
O(n)
• Array: Insertion/deletion takes O(n) but searching takes O(1)
• We want a DS which can optimize most of these operations.
Why Heaps?
• Job scheduler
• Priority queue
• Event driven simulator

• All needs to optimize insert, deleteMax and findMax operations


Any observation?
Any observation?
Heap: An Introduction
• A Heap is a complete (or almost) binary tree-based data structure
with some special properties:
• Max-Heap: The value of each node is less than or equal to the
value of the parent. The greatest value is at the root. The same
property must be true for all subtrees.
• Min-Heap: The value of each node is greater than or equal to
the value of its parent. The smallest value is at the root. The
same property must be true for all subtrees.
(Revision) Complete Binary Tree
• A complete binary tree is a binary tree that satisfies two
properties:
• First, in a complete binary tree, every level, except possibly the last, is
completely filled.
• Second, all nodes appear as far left as possible.
Max Heap
The value of each node is less than or equal to the value of the
parent.
Min Heap
The value of each node is greater than or equal to the value of the
parent.
Heap: An Introduction
• Heaps are not sorted.
• There is no particular relationship between nodes at any level.
• Since heaps are complete binary trees, the height of a tree with N
nodes has O(logN) height.
What if we want to search some
other item in heap?
• Heaps only provide easy access to the smallest/greatest
item.
• Finding other items in the heap takes O(n) time because the
heap is not ordered.
• We must iterate through all the nodes.
Implementation of Heap
• Array is used to implement Heaps (Why?)
• For a node present at the i-th index in an array
[Assume array index starts from 1]:
• It’s parent is at the floor (i-1)/2 index.
• It’s left child is at 2 * i + 1 index.
• It’s right child is at 2 *i + 2 index.
Convert unordered array into a
Complete binary tree
unordered
array as a
Complete
binary tree
(Its not a
heap)
Build a heap

10

21 39

43 80 86

If the array is already sorted in ascending order, its already a MIN-HEAP


If the array is already sorted in descending order, its already a MAX-HEAP
Build Heap
• Method-1: Sort the array and then construct the tree.
• This method is not worth to construct a heap. Why

• Sorting the array in ascending or descending order takes


O(nlogn) time.
• There exist a separate algorithm for building Heap which
runs in O(n) time.
Few algorithms:
• BUILD-HEAP:
o It produces a heap from an unsorted input array
o BUILD-MAX-HEAP and BUILD-MIN-HEAP
o Runs in O(n) time.
• HEAPIFY
o Used to maintain the heap-property
o MAX-HEAPIFY and MIN-HEAPIFY
o Runs in O(logn) time
• HEAP-SORT
o Sorts an array in place
o Runs in O(logn) time
HEAPIFY: Maintains the heap-
property
• When any new element is inserted or deleted into a max
heap, or while building a Max heap from an unsorted array
Heap’s property need to be maintained.
• Heapify is used to maintain the heap property
Heapify: Example
HEAPIFY: Algorithm
Max-HEAPIFY(A,2)
HEAPIFY: Algorithm
Max-HEAPIFY(A,4)
HEAPIFY: Algorithm
Max-HEAPIFY(A,9)
BUILD-MAX-HEAP(A): Building a MAX
heap from an unsorted array
• Given an array A[1…..n], where n = length[A], convert it into max-
heap.
Start from node
at index 5
BUILD-MAX-HEAP(A): Algorithm
Heap Sort

Merge Sort Insertion Sort


O(n log n) (In-Place sorting)

Heap Sort
O(n log n)
In-Place sorting
Heap Sort

• Heap sort is an O(nlogn) comparison based in-place


sorting algorithm.
• Heap sort is not a stable sorting algorithm.
• Heap sort is non-adaptive sorting algorithm
• Heap sort uses a Heap data structure.
• Heap data structure is also used in priority queue.
Given an unsorted array, sort the
array using heap-sort.

Construct a Max-Heap
Largest value of the array is at root Put the largest value at the end of that array.

1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
16 14 10 8 7 9 3 2 4 1 1 14 10 8 7 9 3 2 4 16
Heap sort:
Example
Heap Sort: Algorithm
• What will be the running time of heapsort on an array of
length n that is already sorted in increasing order.

• Sort the array in decreasing order.

You might also like