1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.

12
1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort

Transcript of 1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.

Page 1: 1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.

1

Algorithms

CSCI 235, Fall 2015Lecture 14

Analysis of Heap Sort

Page 2: 1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.

2

Heap SortIn English:

• Build a Heap out of the elements of the array•We know that the maximum value is at A[1], so we swap it with the last element of the heap.• Reduce the size of the heap by 1.• Heapify the new, smaller heap.• Repeat this process until all the nodes have been sorted.

Page 3: 1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.

3

Pseudocode for Heapsort

Heapsort(A)Build-Heap(A)for i length[A] downto 2

do Swap(A, 1, i) heap-size[A] heap-size[A] - 1

Heapify(A, 1)

What is the running time of Heapsort?Must find running times of Build-Heap and Heapify first.

Page 4: 1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.

4

Recall Heapify

Heapify( ) is a function that creates a heap from a node i and two subtrees that are already heaps.

16

3 14

15 6 9 11

7 8 2

1

i=2 3

4 5 6 7

8 9 10

Example: i = 2

Swap the value at node i with the largest of the values at child nodes.If value at node i is larger than those of children, stop.Otherwise, continue with the next level.

Page 5: 1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.

5

Running time of Heapify

Maximum number of swaps in Heapify is the height of the heap.1

3

4 5 6 7

8 9 10

2

Height of n element heap is:

Therefore, the running time of heapify is O(lgn)

Page 6: 1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.

6

Running time of Build heap

Recall that Build-Heap starts with the last element and repeatedly calls Heapify to create heaps from the bottom up.

If the height of a node is given by the longest distance from a leaf to that node, then the number of nodes at a given height h is at most: Why?

Build-Heap calls heapify for each node at a given height:

T(n) = ?

number of nodes at height h

cost of Heapify at height h

Page 7: 1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.

7

Cost of Heap-Sort

Heapsort(A)Build-Heap(A)for i length[A] downto 2 do

Swap(A, 1, i)heap-size[A] heap-size[A] - 1

Heapify(A, 1)

T(n) = ?

Page 8: 1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.

8

Using a Heap in Priority Queues

A priority queue is a queue in which the element with the highest value is retrieved first. WIPO: Whatever in, Priority out.

Priority Queue Operations:Insert(S, x)

Inserts element x into set SMaximum(S)

Returns the element of S with the largest value (or key)Extract-Max(S)

Removes and returns the element of S with the largest value.

Heaps are useful data structures for priority queue functions.

Page 9: 1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.

9

Extracting the Maximum

Idea: Extract the maximum element, reduce the heap size by 1, then heapify.

Heap-Extract-Max(A)if heap_size[A] < 1 then error "heap underflow" max A[1] { root value is max } A[1] A[heap_size[A]] { put A[heap_size[A]] in root} heap_size[A] heap_size[A] - 1 { decrease heap size } Heapify(A, 1) { enforce heap property } return max

Page 10: 1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.

10

Example

16

12 8

3 2

1

3

4 5

We will work this through in class.

Page 11: 1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.

11

Heap-InsertIdea: To insert an element at the proper place in the heap, traverse the tree from leaf to root and find the correct place.

HeapInsert(A, key)heap_size[A] heap_size[A] + 1 { increase size of heap }i heap-size[A]while i > 1 and A[Parent(i)] < key do

A[i] A[Parent(i)]i Parent(i)

A[i] key

Page 12: 1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.

12

Example

16

14 10

8 7 9 3

2 4 1

1

3

4 5 6 7

8 9 10

Insert the number, 15.

We will work through this in class.