25
Chapter 6 Heapsort Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides fro m Hsu, Lih-Hsing, as well as various materials from the we b.

Chapter 6

  • Upload
    alida

  • View
    22

  • Download
    0

Embed Size (px)

DESCRIPTION

Chapter 6. Heapsort Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the web. Why sorting. 1. Sometimes the need to sort information is inherent in the application. - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 6

Chapter 6

Heapsort Lee, Hsiu-Hui

Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the we

b.

Page 2: Chapter 6

20071005 chap06 Hsiu-Hui Lee 2

Why sorting 1. Sometimes the need to sort information is

inherent in the application. 2. Algorithms often use sorting as a key subroutine. 3. There is a wide variety of sorting algorithms, and

they use rich set of techniques. 4. Sorting problem has a nontrivial lower bound 5. Many engineering issues come to fore when

implementing sorting algorithms.

Page 3: Chapter 6

20071005 chap06 Hsiu-Hui Lee 3

Sorting algorithm

• Insertion sort : • In place: only a constant number of elemen

ts of the input array are even sorted outside the array.

• Merge sort : • not in place.

• Heap sort : (chapter 6) • Sorts n numbers in place in O(n lgn)

Page 4: Chapter 6

20071005 chap06 Hsiu-Hui Lee 4

Sorting algorithm

• Quick sort : (chapter 7)• worst time complexity O(n2)• Average time complexity O(n lg n)

• Decision tree model : (chapter 8)• Lower bound O (n lg n)• Counting sort• Radix sort

• Order statistics

Page 5: Chapter 6

20071005 chap06 Hsiu-Hui Lee 5

6.1 Heaps (Binary heap) • The binary heap data structure is an array

object that can be viewed as a complete tree.

Parent(i)return 

Left(i)return 2i 

Right(i)return 2i+1

2/i

Page 6: Chapter 6

20071005 chap06 Hsiu-Hui Lee 6

Heap property

• Max-heap : A[Parent(i)] A[i]• Min-heap : A[Parent(i)] ≤ A[i]• The height of a node in a tree: the number of ed

ges on the longest simple downward path from the node to a leaf.

• The height of a tree: the height of the root • The height of a heap: O(lg n).

Page 7: Chapter 6

20071005 chap06 Hsiu-Hui Lee 7

Basic procedures on heap

• Max-Heapify• Build-Max-Heap• Heapsort • Max-Heap-Insert • Heap-Extract-Max • Heap-Increase-Key• Heap-Maximum

Page 8: Chapter 6

20071005 chap06 Hsiu-Hui Lee 8

6.2 Maintaining the heap property

Max-Heapify(A, i)

• To maintain the max-heap property.

• Assume that the binary trees rooted at Left

(i) and Right(i) are heaps, but that A[i]

may be smaller than its children, thus violatin

g the heap property.

Page 9: Chapter 6

20071005 chap06 Hsiu-Hui Lee 9

Max-Heapify(A,i)

1 l Left(i) 2 r Right(i) 3 if l ≤ heap-size[A] and A[l] > A[i]

4 then largest l 5 else largest i 6 if r ≤ heap-size[A] and A[r] > A[largest]

7 then largest r 8 if largest i 9 then exchange A[i] A[largest]

10 Max-Heapify(A, largest)

Alternatively O(h) (h: height)

)(lg)()1()3

2()( nOnT

nTnT

Page 10: Chapter 6

20071005 chap06 Hsiu-Hui Lee 10

Max-Heapify(A,2)heap-size[A] = 10

Max-Heapify(A,2)

-> Max-Heapify(A,4)

-> Max-Heapify(A,9)

Page 11: Chapter 6

20071005 chap06 Hsiu-Hui Lee 11

6.3 Building a Heap Build-Max-Heap(A)

• To produce a max-heap from an unordered in

put array.

Build-Max-Heap(A)1 heap-size[A] length[A]2 for i length[A]/2 downto 13 do Max-Heapify(A, i)

Page 12: Chapter 6

20071005 chap06 Hsiu-Hui Lee 12

Build-Max-Heap(A)heap-size[A] = 10

i=5Max-Heapify(A, i)

i=4Max-Heapify(A, i)

Page 13: Chapter 6

20071005 chap06 Hsiu-Hui Lee 13

i=3Max-Heapify(A, i)

i=2Max-Heapify(A, i)

Page 14: Chapter 6

20071005 chap06 Hsiu-Hui Lee 14

i=1Max-Heapify(A, i)

Page 15: Chapter 6

20071005 chap06 Hsiu-Hui Lee 15

)lg( nnO ?

)()

2()

2(

))1(

( 22

)2

()(2

0

lg

0

02

0

lg

0

lg

01

nOh

nOh

nO

x

xkx

h

hnOhO

n

hh

n

hh

k

k

hh

n

hh

n

hh

Page 16: Chapter 6

20071005 chap06 Hsiu-Hui Lee 16

6.4 Heapsort algorithm

Heapsort(A)

• To sort an array in place.

Heapsort(A)1 Build-Max-Heap(A)2 for i length[A] down to 23 do exchange A[1]A[i]4 heap-size[A] heap-size[A]-15 Max-Heapify(A,1)

Page 17: Chapter 6

20071005 chap06 Hsiu-Hui Lee 17

Operation of Heapsort

Page 18: Chapter 6

20071005 chap06 Hsiu-Hui Lee 18

Analysis: O(n lg n)

Page 19: Chapter 6

20071005 chap06 Hsiu-Hui Lee 19

6.5 Priority Queues• A priority queue is a data structure that

maintain a set S of elements, each with an associated value call a key.

• A max-priority queue support the following operations:Insert(S, x) O(lg n)

inserts the element x into the set S.Maximum(S) O(1)

returns the element of S with the largest key.

Page 20: Chapter 6

20071005 chap06 Hsiu-Hui Lee 20

Extract-Max(S) O(lg n)removes and returns the element of S with

the largest key.

Increase-Key(S,x,k) O(lg n)increases the value of element x’s key to th

e new value k, where k x’s current key value

Page 21: Chapter 6

20071005 chap06 Hsiu-Hui Lee 21

Heap-Maximum

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

Page 22: Chapter 6

20071005 chap06 Hsiu-Hui Lee 22

Heap_Extract-Max Heap_Extract-Max(A) 1 if heap-size[A] < 12 then error “heap underflow”3 max A[1]4 A[1] A[heap-size[A]]5 heap-size[A] heap-size[A] - 16 Max-Heapify(A, 1)7 return max

Page 23: Chapter 6

20071005 chap06 Hsiu-Hui Lee 23

Heap-Increase-KeyHeap-Increase-Key (A, i, key) 1 if key < A[i]

2 then error “new key is smaller than

current key”

3 A[i] key4 while i > 1 and A[Parent(i)] < A[i]

5 do exchange A[i] A[Parent(i)]

6 i Parent(i)

Page 24: Chapter 6

20071005 chap06 Hsiu-Hui Lee 24

Heap-Increase-Key(A,i,15)

Page 25: Chapter 6

20071005 chap06 Hsiu-Hui Lee 25

Heap_Insert

Heap_Insert(A, key)

1 heap-size[A] heap-size[A] + 12 A[heap-size[A]] -∞3 Heap-Increase-Key(A, heap-size

[A], key)