Heapsort O(n lg n) worst case Another design paradigm –Use of a data structure (heap) to manage...

31
Heapsort • O(n lg n) worst case Another design paradigm Use of a data structure (heap) to manage information during execution of algorithm Comparision-based Sorting Algorithm Analysis of Algorithms 1

Transcript of Heapsort O(n lg n) worst case Another design paradigm –Use of a data structure (heap) to manage...

Heapsort

• O(n lg n) worst case• Another design paradigm

– Use of a data structure (heap) to manage information during execution of algorithm

• Comparision-based Sorting Algorithm

Analysis of Algorithms 1

Heap Data Structure

Analysis of Algorithms 2

Heap Property

Analysis of Algorithms 3

A Heap Example

Analysis of Algorithms 4

Heap Data Structure

Analysis of Algorithms 5

Heap Operations

Analysis of Algorithms 6

Heap Operations

Analysis of Algorithms 7

Maintaining Heap

Analysis of Algorithms 8

Runtime Analysis of HEAPIFY

Analysis of Algorithms 9

Formal Runtime Analysis of HEAPIFY

Analysis of Algorithms 10

Formal Runtime Analysis of HEAPIFY

Analysis of Algorithms 11

Building Heap

Analysis of Algorithms 12

Proof of Lemma

Analysis of Algorithms 13

Building Heap

Analysis of Algorithms 14

Build Heap - Example

Analysis of Algorithms 15

Build Heap - Example

Analysis of Algorithms 16

BuildHeap – Tighter Running Time Analysis

Analysis of Algorithms 17

BuildHeap – Tighter Running Time Analysis

Analysis of Algorithms 18

BuildHeap – Tighter Running Time Analysis

Analysis of Algorithms 19

BuildHeap – Tighter Running Time Analysis

Analysis of Algorithms 20

HEAPSORT

Analysis of Algorithms 21

HEAPSORT

Analysis of Algorithms 22

Heapsort - Example

Analysis of Algorithms 23

Heapsort - Example

Analysis of Algorithms 24

Heapsort - Example

Analysis of Algorithms 25

Heapsort – Running Time Analysis

Analysis of Algorithms 26

How fast can we sort?

• The comparison sorting algorithms such as insertion sort, mergesort, quicksort and heapsort only use comparisons to determine the relative order of elements.

• The best worst-case running time that we’ve seen for comparison sorting is O(n lg n) .

Is O(n lg n) the best we can do?

• Decision trees can help us answer this question.

Analysis of Algorithms 27

Decision-tree example

Analysis of Algorithms 28

Decision-tree example

Analysis of Algorithms 29

Decision-tree model

Analysis of Algorithms 30

Lower bound for decision-tree sorting

Analysis of Algorithms 31

Corollary. Heapsort and merge sort are asymptotically optimal comparison sorting algorithms.