0% found this document useful (0 votes)
62 views38 pages

Heap Sort

This document discusses binary heaps and how they can be used to implement priority queues. It explains that a binary heap is a complete binary tree stored in an array. It also describes min-heaps and max-heaps, which satisfy the min-heap or max-heap property respectively. The key operations for heaps are building a heap from an array in O(n) time using BuildHeap(), and heapifying a subtree in O(log n) time using Heapify(). Heapsort uses these operations to sort an array in O(n log n) time. Finally, priority queues can be implemented efficiently using heaps, with operations like Insert(), Maximum(), and ExtractMax() taking advantage of the heap

Uploaded by

nurulalomador
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)
62 views38 pages

Heap Sort

This document discusses binary heaps and how they can be used to implement priority queues. It explains that a binary heap is a complete binary tree stored in an array. It also describes min-heaps and max-heaps, which satisfy the min-heap or max-heap property respectively. The key operations for heaps are building a heap from an array in O(n) time using BuildHeap(), and heapifying a subtree in O(log n) time using Heapify(). Heapsort uses these operations to sort an array in O(n log n) time. Finally, priority queues can be implemented efficiently using heaps, with operations like Insert(), Maximum(), and ExtractMax() taking advantage of the heap

Uploaded by

nurulalomador
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/ 38

Heap Sort and Priority

Queue
CSE 2215 - Lecture 12 - Summer 2023
Instructor : Fahmid Al Rifat, Lecturer, Dept. of CSE , UIU

[email protected] 1
Binary Heaps

 The (binary) heap data structure is an array object that can be viewed as a complete binary tree
 Each node of the tree corresponds to an element of the array that stores the value in the node.

 The tree is completely filled on all levels except possibly the lowest, where it is filled from the left up

to a point.

2
Binary Heaps

3
Types of Binary Heaps

4
The Min-Heap Property

 Min-Heaps satisfy the heap property:


A[Parent(i)] ≤ A[i] ; for all nodes i > 1
 The value of a node is at least the value of its parent

 The smallest element in a min-heap is stored at the root

 Where is the largest element ???

 Ans: At one of the leaves [leaf indices are n/2+1 to n]

5
The Max-Heap Property

 Max-Heaps satisfy the heap property:


A[Parent(i)]  A[i] ; for all nodes i > 1
 The value of a node is at most the value of its parent

 The largest element in a max-heap is stored at the root

 Where is the smallest element ???

 Ans: At one of the leaves [leaf indices are n/2+1 to n]

6
Max-Heap Operations: Max-Heapify()
 Max-Heapify(): maintain the max-heap property
 Given: a node i in the heap with children l and r

: two subtrees rooted at l and r, assumed to be heaps


 Problem: The subtree rooted at i may violate the heap property (How?)

 Action: let the value of the parent node “float down” so subtree at i satisfies the heap property

 What will be the basic operation between i, l, and r?

7
Max-Heap Operations: Max-Heapify()

8
Heapify() - Example

9
Heapify() - Example

10
Heapify() - Example

11
Heapify() - Example

12
Heapify() - Example

13
Heapify() - Example

14
Heapify() - Example

15
Heapify() - Example

16
Heapify() - Example

17
Analyzing Heapify()
Fixing up relationships among the elements A[i], A[l], and A[r] takes O(1) time
Ifthe heap at i has n elements, at most how many elements can the subtrees at l or r
have?

Answer: 2n/3 (worst case: bottom row half full)

So time taken by Heapify() is given by


T(n) ≤ T(2n/3) + (1)
18
Analyzing Heapify()
So we have
• T(n) ≤ T(2n/3) + (1)

Solving the recurrence, we have


• T(n) = O(log n)

Thus, Heapify() takes O(h) time for a node at height h.

19
Heap Operations: BuildHeap()

We can build a heap in a bottom-up manner by running Heapify() on successive


subarrays
 Fact: for array of length n, all elements in the range

A[ n/2 + 1 ... n] already satisfies Heap Property


(Why?)
 So

Walk backwards through the array from n/2 to 1,


calling Heapify() on each node.
Order of processing guarantees that the children

of node i are heaps when i is processed

20
Heap Operations: BuildHeap()
• Converts an unorganized array A into a max-heap.

21
BuildHeap(): Example

22
BuildHeap(): Example

23
BuildHeap(): Example

24
BuildHeap(): Example 2

 Workthrough example
A = {14, 11, 33, 22, 56, 49, 30, 24, 18, 37}
Construct a Min-Heap from the given array using Build-Heap function.

25
Analyzing BuildHeap()

Each call to Heapify() takes O(log n) time


There are O(n) such calls (specifically, n/2 )
Thus the running time is O(n log n)
Is this a correct asymptotic upper bound?

Is this an asymptotically tight bound?

A tighter bound is O(n)


How can this be? Is there a flaw in the above reasoning?

26
Analyzing BuildHeap(): Tight

To Heapify() a subtree takes O(h) time, where h is the height of the
subtree
h = O(log m), m = # nodes in the subtree

The height of most subtrees is small

Fact: an n-element heap has at most n/2h+1 nodes of height h


Prove that BuildHeap() takes O(n) time

27
HeapSort

Given BuildHeap(), a sorting algorithm can easily be constructed:


Maximum element is at A[1]

Discard by swapping with element at A[n]


Decrement heap_size[A]

A[n] now contains correct value

Restore heap property at A[1] by calling Heapify()

Repeat, always swapping A[1] for A[heap_size(A)]

28
HeapSort

Heapsort(A)
{
BuildHeap(A);
for (i = length(A) downto 2)
{
Swap(A[1], A[i]);
heap_size(A) = heap_size(A) - 1;
Heapify(A, 1);
}
}

29
HeapSort

Work through example


A = {14, 11, 33, 22, 56, 49, 30, 24, 18, 37}

30
Analyzing HeapSort

The call to BuildHeap() takes O(n) time


Each of the (n – 1) calls to Heapify() takes O(log n) time
Thus the total time taken by HeapSort()
= O(n) + (n - 1) O(log n)
= O(n) + O(n log n)
= O(n log n)

31
Priority Queue

A queue that is ordered according to some priority value


The heap data structure is incredibly useful for implementing priority queues
A data structure for maintaining a set S of elements, each with an associated value

or key
Supports the operations Insert(), Maximum(), and ExtractMax()

32
Priority Queue Operations

• Insert( S, x ) – Inserts element x into set S, according to its priority

• Maximum( S ) – Returns, but does not remove, the element of S with the largest key

• Extract-Max( S ) – Removes and returns the element of S with the largest key

• Increase-Key( S, x, k ) – Increases the value of element x’s key to the new value k

 How could we implement these operations using a heap?

33
Priority Queue Operations

34
Priority Queue Operations

35
Priority Queue Operations

36
Priority Queue Operations

37
Thank You

38

You might also like