6331 - algorithms, cse, osu heapsort · 2017-08-16 · i procedure max-heapify (auxiliary...

57
6331 - Algorithms, CSE, OSU Heapsort Instructor: Anastasios Sidiropoulos

Upload: others

Post on 13-Jul-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

6331 - Algorithms, CSE, OSU

Heapsort

Instructor: Anastasios Sidiropoulos

Page 2: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Sorting

Given an array of integers A[1 . . . n], rearrange its elements so that

A[1] ≤ A[2] ≤ . . . ≤ A[n].

Page 3: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

A simple sorting algorithm

Bubble-Sort

repeatswapped = falsefor i = 1 to n − 1 doif A[i ] > A[i + 1] thenswap(A[i ],A[i + 1])swapped = true

end ifend for

until not swapped

What is the worst-case time complexity of this algorithm?

We can do much better!

Page 4: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

A simple sorting algorithm

Bubble-Sort

repeatswapped = falsefor i = 1 to n − 1 doif A[i ] > A[i + 1] thenswap(A[i ],A[i + 1])swapped = true

end ifend for

until not swapped

What is the worst-case time complexity of this algorithm?

We can do much better!

Page 5: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

A simple sorting algorithm

Bubble-Sort

repeatswapped = falsefor i = 1 to n − 1 doif A[i ] > A[i + 1] thenswap(A[i ],A[i + 1])swapped = true

end ifend for

until not swapped

What is the worst-case time complexity of this algorithm?

We can do much better!

Page 6: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Heaps

A Heap is a data structure representing a full binary tree.

I A heap is stored in an array A[1 . . . n].

I The root is A[1].

I parent(i) = i/2.

I left-child(i) = 2i .

I right-child(i) = 2i + 1.

Page 7: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Heaps

A Heap is a data structure representing a full binary tree.

I A heap is stored in an array A[1 . . . n].

I The root is A[1].

I parent(i) = i/2.

I left-child(i) = 2i .

I right-child(i) = 2i + 1.

Page 8: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Max-HeapsFor all nodes other than the root, we have A[parent(i)] ≥ A[i ]

I Where is the maximum element in the tree?

I Where is the maximum element in the array?

I Where is the minimum element in the tree?

I Where is are the leaves in the array?

Page 9: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Max-HeapsFor all nodes other than the root, we have A[parent(i)] ≥ A[i ]

I Where is the maximum element in the tree?

I Where is the maximum element in the array?

I Where is the minimum element in the tree?

I Where is are the leaves in the array?

Page 10: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Max-HeapsFor all nodes other than the root, we have A[parent(i)] ≥ A[i ]

I Where is the maximum element in the tree?

I Where is the maximum element in the array?

I Where is the minimum element in the tree?

I Where is are the leaves in the array?

Page 11: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Max-HeapsFor all nodes other than the root, we have A[parent(i)] ≥ A[i ]

I Where is the maximum element in the tree?

I Where is the maximum element in the array?

I Where is the minimum element in the tree?

I Where is are the leaves in the array?

Page 12: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Max-HeapsFor all nodes other than the root, we have A[parent(i)] ≥ A[i ]

I Where is the maximum element in the tree?

I Where is the maximum element in the array?

I Where is the minimum element in the tree?

I Where is are the leaves in the array?

Page 13: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Max-HeapsFor all nodes other than the root, we have A[parent(i)] ≥ A[i ]

I Where is the maximum element in the tree?

I Where is the maximum element in the array?

I Where is the minimum element in the tree?

I Where is are the leaves in the array?

Page 14: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Height

The height of a node i is the maximum number of edges on a pathfrom i to a leaf.

The height of a tree is the height of its root.

What is the height of a heap?

Page 15: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Height

The height of a node i is the maximum number of edges on a pathfrom i to a leaf.

The height of a tree is the height of its root.

What is the height of a heap?

Page 16: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Height

The height of a node i is the maximum number of edges on a pathfrom i to a leaf.

The height of a tree is the height of its root.

What is the height of a heap?

Page 17: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Building and using heaps

I Procedure Max-Heapify (auxiliary procedure)

I Procedure Build-Max-Heap (building a max-heap)

I Procedure Heap-Sort (sorting using a heap)

Page 18: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Maintaining the max-heap property

Suppose that the subtrees rooted at left-child(i) and right-child(i)are max-heaps.

However, i might violate the max-heap property.E.g., A[i ] < A[left-child(i)].

How can we enforce the max-heap property?

Page 19: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Maintaining the max-heap property

Suppose that the subtrees rooted at left-child(i) and right-child(i)are max-heaps.

However, i might violate the max-heap property.E.g., A[i ] < A[left-child(i)].

How can we enforce the max-heap property?

Page 20: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Maintaining the max-heap property

Procedure Max-Heapify(A, i)l = left-child(i)r = right-child(i)if l ≤ n and A[l ] > A[i ]largest = l

else largest = iif r ≤ n and A[r ] > largestlargest = r

if largest 6= iexchange A[i ] with A[largest]Max-Heapify(A, largest)

Page 21: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Running time of Max-Heapify

I What is the running time of Max-Heapify(A, i)?

I Total time spent in Max-Heapify is at most O(1) + the timespent in the recursive call Max-Heapify(A, largest).

I Total running time of the recursion?

I What is the depth of the recursion?

I Worst-case depth of recursion = height of i .

I Worst-case running time is O(height(i)).

I Worst-case running time is O(log(n)).

I Is this tight?

Page 22: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Running time of Max-Heapify

I What is the running time of Max-Heapify(A, i)?

I Total time spent in Max-Heapify is at most O(1) + the timespent in the recursive call Max-Heapify(A, largest).

I Total running time of the recursion?

I What is the depth of the recursion?

I Worst-case depth of recursion = height of i .

I Worst-case running time is O(height(i)).

I Worst-case running time is O(log(n)).

I Is this tight?

Page 23: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Running time of Max-Heapify

I What is the running time of Max-Heapify(A, i)?

I Total time spent in Max-Heapify is at most O(1) + the timespent in the recursive call Max-Heapify(A, largest).

I Total running time of the recursion?

I What is the depth of the recursion?

I Worst-case depth of recursion = height of i .

I Worst-case running time is O(height(i)).

I Worst-case running time is O(log(n)).

I Is this tight?

Page 24: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Running time of Max-Heapify

I What is the running time of Max-Heapify(A, i)?

I Total time spent in Max-Heapify is at most O(1) + the timespent in the recursive call Max-Heapify(A, largest).

I Total running time of the recursion?

I What is the depth of the recursion?

I Worst-case depth of recursion = height of i .

I Worst-case running time is O(height(i)).

I Worst-case running time is O(log(n)).

I Is this tight?

Page 25: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Running time of Max-Heapify

I What is the running time of Max-Heapify(A, i)?

I Total time spent in Max-Heapify is at most O(1) + the timespent in the recursive call Max-Heapify(A, largest).

I Total running time of the recursion?

I What is the depth of the recursion?

I Worst-case depth of recursion = height of i .

I Worst-case running time is O(height(i)).

I Worst-case running time is O(log(n)).

I Is this tight?

Page 26: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Running time of Max-Heapify

I What is the running time of Max-Heapify(A, i)?

I Total time spent in Max-Heapify is at most O(1) + the timespent in the recursive call Max-Heapify(A, largest).

I Total running time of the recursion?

I What is the depth of the recursion?

I Worst-case depth of recursion = height of i .

I Worst-case running time is O(height(i)).

I Worst-case running time is O(log(n)).

I Is this tight?

Page 27: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Running time of Max-Heapify

I What is the running time of Max-Heapify(A, i)?

I Total time spent in Max-Heapify is at most O(1) + the timespent in the recursive call Max-Heapify(A, largest).

I Total running time of the recursion?

I What is the depth of the recursion?

I Worst-case depth of recursion = height of i .

I Worst-case running time is O(height(i)).

I Worst-case running time is O(log(n)).

I Is this tight?

Page 28: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Running time of Max-Heapify

I What is the running time of Max-Heapify(A, i)?

I Total time spent in Max-Heapify is at most O(1) + the timespent in the recursive call Max-Heapify(A, largest).

I Total running time of the recursion?

I What is the depth of the recursion?

I Worst-case depth of recursion = height of i .

I Worst-case running time is O(height(i)).

I Worst-case running time is O(log(n)).

I Is this tight?

Page 29: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Building a heap

Procedure Build-Max-Heap(A)for i = bn/2c downto 1Max-Heapify(A, i)

Loop invariant:At the start of each iteration, each node i + 1, i + 2, . . . , n is theroot of a max-heap.

I Initialization: i = bn/2c. The nodes bn/2c+ 1, . . . , n areleaves, and so they are max-heaps.

I Maintenance: By the loop invariant, the children of i areroots of max-heaps. Therefore, running Max-Heapify makes ithe root of a max-heap.

I Termination: i = 1. By the loop invariant, 1 is the root of aheap.

Page 30: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Building a heap

Procedure Build-Max-Heap(A)for i = bn/2c downto 1Max-Heapify(A, i)

Loop invariant:At the start of each iteration, each node i + 1, i + 2, . . . , n is theroot of a max-heap.

I Initialization: i = bn/2c. The nodes bn/2c+ 1, . . . , n areleaves, and so they are max-heaps.

I Maintenance: By the loop invariant, the children of i areroots of max-heaps. Therefore, running Max-Heapify makes ithe root of a max-heap.

I Termination: i = 1. By the loop invariant, 1 is the root of aheap.

Page 31: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Building a heap

Procedure Build-Max-Heap(A)for i = bn/2c downto 1Max-Heapify(A, i)

Loop invariant:At the start of each iteration, each node i + 1, i + 2, . . . , n is theroot of a max-heap.

I Initialization: i = bn/2c. The nodes bn/2c+ 1, . . . , n areleaves, and so they are max-heaps.

I Maintenance: By the loop invariant, the children of i areroots of max-heaps. Therefore, running Max-Heapify makes ithe root of a max-heap.

I Termination: i = 1. By the loop invariant, 1 is the root of aheap.

Page 32: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Building a heap

Procedure Build-Max-Heap(A)for i = bn/2c downto 1Max-Heapify(A, i)

Loop invariant:At the start of each iteration, each node i + 1, i + 2, . . . , n is theroot of a max-heap.

I Initialization: i = bn/2c. The nodes bn/2c+ 1, . . . , n areleaves, and so they are max-heaps.

I Maintenance: By the loop invariant, the children of i areroots of max-heaps. Therefore, running Max-Heapify makes ithe root of a max-heap.

I Termination: i = 1. By the loop invariant, 1 is the root of aheap.

Page 33: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Building a heap

Procedure Build-Max-Heap(A)for i = bn/2c downto 1Max-Heapify(A, i)

Loop invariant:At the start of each iteration, each node i + 1, i + 2, . . . , n is theroot of a max-heap.

I Initialization: i = bn/2c. The nodes bn/2c+ 1, . . . , n areleaves, and so they are max-heaps.

I Maintenance: By the loop invariant, the children of i areroots of max-heaps. Therefore, running Max-Heapify makes ithe root of a max-heap.

I Termination: i = 1. By the loop invariant, 1 is the root of aheap.

Page 34: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Running time of Max-Heapify

I Each call to Max-Heapify takes time O(log n).

I There are O(n) calls to Max-Heapify.

I Total running time O(n · log(n)).I This is not asymptotically tight!

Page 35: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Running time of Max-Heapify

I Each call to Max-Heapify takes time O(log n).

I There are O(n) calls to Max-Heapify.

I Total running time O(n · log(n)).I This is not asymptotically tight!

Page 36: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Running time of Max-Heapify

I Each call to Max-Heapify takes time O(log n).

I There are O(n) calls to Max-Heapify.

I Total running time O(n · log(n)).

I This is not asymptotically tight!

Page 37: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Running time of Max-Heapify

I Each call to Max-Heapify takes time O(log n).

I There are O(n) calls to Max-Heapify.

I Total running time O(n · log(n)).I This is not asymptotically tight!

Page 38: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Running time of Max-Heapify: Better analysis

I Each call Max-Heapify(A, i) takes time O(height(i)).

I A heap has height blog(n)c.I There are at most dn/2h+1e nodes of height h.I Total running time:

blog nc∑h=0

⌈ n

2h+1

⌉O(h) = O

(n

∞∑h=0

h

2h

)= O(n).

Page 39: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Running time of Max-Heapify: Better analysis

I Each call Max-Heapify(A, i) takes time O(height(i)).

I A heap has height blog(n)c.

I There are at most dn/2h+1e nodes of height h.I Total running time:

blog nc∑h=0

⌈ n

2h+1

⌉O(h) = O

(n

∞∑h=0

h

2h

)= O(n).

Page 40: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Running time of Max-Heapify: Better analysis

I Each call Max-Heapify(A, i) takes time O(height(i)).

I A heap has height blog(n)c.I There are at most dn/2h+1e nodes of height h.

I Total running time:

blog nc∑h=0

⌈ n

2h+1

⌉O(h) = O

(n

∞∑h=0

h

2h

)= O(n).

Page 41: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Running time of Max-Heapify: Better analysis

I Each call Max-Heapify(A, i) takes time O(height(i)).

I A heap has height blog(n)c.I There are at most dn/2h+1e nodes of height h.I Total running time:

blog nc∑h=0

⌈ n

2h+1

⌉O(h) = O

(n

∞∑h=0

h

2h

)= O(n).

Page 42: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Sorting using a heap

Procedure Heapsort(A)Build-Max-Heap(A)for i = A.length downto 2exchange A[1] with A[i ]A.heap-size = A.heap-size− 1Max-Heapify(A, 1)

I Build-Max-Heap takes time O(n).

I There are n − 1 calls to Max-Heapify.

I Each call to Max-Heapify takes time O(log n).

I Total running time O(n log(n)).

Page 43: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Sorting using a heap

Procedure Heapsort(A)Build-Max-Heap(A)for i = A.length downto 2exchange A[1] with A[i ]A.heap-size = A.heap-size− 1Max-Heapify(A, 1)

I Build-Max-Heap takes time O(n).

I There are n − 1 calls to Max-Heapify.

I Each call to Max-Heapify takes time O(log n).

I Total running time O(n log(n)).

Page 44: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Sorting using a heap

Procedure Heapsort(A)Build-Max-Heap(A)for i = A.length downto 2exchange A[1] with A[i ]A.heap-size = A.heap-size− 1Max-Heapify(A, 1)

I Build-Max-Heap takes time O(n).

I There are n − 1 calls to Max-Heapify.

I Each call to Max-Heapify takes time O(log n).

I Total running time O(n log(n)).

Page 45: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Sorting using a heap

Procedure Heapsort(A)Build-Max-Heap(A)for i = A.length downto 2exchange A[1] with A[i ]A.heap-size = A.heap-size− 1Max-Heapify(A, 1)

I Build-Max-Heap takes time O(n).

I There are n − 1 calls to Max-Heapify.

I Each call to Max-Heapify takes time O(log n).

I Total running time O(n log(n)).

Page 46: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Sorting using a heap

Procedure Heapsort(A)Build-Max-Heap(A)for i = A.length downto 2exchange A[1] with A[i ]A.heap-size = A.heap-size− 1Max-Heapify(A, 1)

I Build-Max-Heap takes time O(n).

I There are n − 1 calls to Max-Heapify.

I Each call to Max-Heapify takes time O(log n).

I Total running time O(n log(n)).

Page 47: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Priority queues

A priority queue is a data structure for maintaining a set S ofelements, each having a key.

There are max-priority queues, and min-priority queues.

Operations of a max-priority queue:

I Insert(S , x): S = S ∪ {x}.I Maximum(S): Return the element in S with the maximum

key.

I Extract-Max(S): Removes and returns the element in S withthe maximum key.

I Increase-Key(S , x , k): Increases the value of the key of x to k ,assuming that k is larger than the current value.

Page 48: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Priority queues

A priority queue is a data structure for maintaining a set S ofelements, each having a key.

There are max-priority queues, and min-priority queues.

Operations of a max-priority queue:

I Insert(S , x): S = S ∪ {x}.I Maximum(S): Return the element in S with the maximum

key.

I Extract-Max(S): Removes and returns the element in S withthe maximum key.

I Increase-Key(S , x , k): Increases the value of the key of x to k ,assuming that k is larger than the current value.

Page 49: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Priority queues

A priority queue is a data structure for maintaining a set S ofelements, each having a key.

There are max-priority queues, and min-priority queues.

Operations of a max-priority queue:

I Insert(S , x): S = S ∪ {x}.I Maximum(S): Return the element in S with the maximum

key.

I Extract-Max(S): Removes and returns the element in S withthe maximum key.

I Increase-Key(S , x , k): Increases the value of the key of x to k ,assuming that k is larger than the current value.

Page 50: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Implementing a max-priority queue using a max-heap

Procedure Heap-Maximum(A)

return A[1]

Page 51: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Implementing a max-priority queue using a max-heap

Procedure Heap-Maximum(A)return A[1]

Page 52: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Implementing a max-priority queue using a max-heap

Procedure Heap-Extract-Max(A)

if n < 1error “empty heap”

max = A[1]A[1] = A[n]n = n − 1Max-Heapify(A, 1)return max

Page 53: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Implementing a max-priority queue using a max-heap

Procedure Heap-Extract-Max(A)if n < 1error “empty heap”

max = A[1]A[1] = A[n]n = n − 1Max-Heapify(A, 1)return max

Page 54: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Implementing a max-priority queue using a max-heap

Procedure Heap-Increase-Key(A, i , key)

if key < A[i ]error

A[i ] = keywhile i > 1 and A[parent(i)] < A[i ]exchange A[i ] with A[parent(i)]i = parent(i)

Page 55: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Implementing a max-priority queue using a max-heap

Procedure Heap-Increase-Key(A, i , key)if key < A[i ]error

A[i ] = keywhile i > 1 and A[parent(i)] < A[i ]exchange A[i ] with A[parent(i)]i = parent(i)

Page 56: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Implementing a max-priority queue using a max-heap

Procedure Max-Heap-Insert(A, key)

n = n + 1A[n] = −∞Heap-Increase-Key(A, n, key)

Page 57: 6331 - Algorithms, CSE, OSU Heapsort · 2017-08-16 · I Procedure Max-Heapify (auxiliary procedure) I Procedure Build-Max-Heap (building a max-heap) I Procedure Heap-Sort (sorting

Implementing a max-priority queue using a max-heap

Procedure Max-Heap-Insert(A, key)n = n + 1A[n] = −∞Heap-Increase-Key(A, n, key)