Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n)...

39
Heapsort A minimalist's approach Jeff Chastine

description

The Heap A binary heap is a nearly complete binary tree Implemented as an array A Two similar attributes: – length[A] is the size (number of slots) in A – heap-size[A] is the number of elements in A – Thus, heap-size[A]  length[A] – Also, no element past A[heap-size[A]] is an element Jeff Chastine

Transcript of Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n)...

Page 1: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

Heapsort

A minimalist's approach

Jeff Chastine

Page 2: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

Heapsort

• Like MERGESORT, it runs in O(n lg n)• Unlike MERGESORT, it sorts in place• Based off of a “heap”, which has several uses• The word “heap” doesn’t refer to memory

management

Jeff Chastine

Page 3: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

The Heap

• A binary heap is a nearly complete binary tree• Implemented as an array A• Two similar attributes:– length[A] is the size (number of slots) in A– heap-size[A] is the number of elements in A– Thus, heap-size[A] length[A]– Also, no element past A[heap-size[A]] is an

element

Jeff Chastine

Page 4: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

The Heap

• Can be a min-heap or a max-heap

87

51 67

55 434125

21 17 33 35

87

87 51 67 25 41 55 43 21 17 33 35Jeff Chastine

Page 5: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

Simple Functions

PARENT(i)return (i/2)

LEFT(i)return (2i)

RIGHT(i)return (2i + 1)

Jeff Chastine

Page 6: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

Properties

• Max-heap property:– A[PARENT(i)] A[i]

• Min-heap property:– A[PARENT(i)] A[i]

• Max-heaps are used for sorting• Min-heaps are used for priority queues (later)• We define the height of a node to be the longest

path from the node to a leaf.• The height of the tree is (lg n)

Jeff Chastine

Page 7: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

MAX-HEAPIFY

• This is the heart of the algorithm• Determines if an individual node is smaller

than its children• Parent swaps with largest child if that child is

larger• Calls itself recursively• Runs in O(lg n) or O(h)

Jeff Chastine

Page 8: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

HEAPIFYMAX-HEAPIFY (A, i)l ← LEFT (i)r ← RIGHT(i)if l ≤ heap-size[A] and A[l] > A[i]then largest ← lelse largest ← iif r ≤ heap-size[A] and A[r]>A[largest]then largest ← rif largest ≠ ithen exchange A[i] with A[largest]MAX-HEAPIFY (A, largest)

Jeff Chastine

Page 9: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

16

4 10

9 314 7

2 8 1

Jeff Chastine

Page 10: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

16

14 10

9 34 7

2 8 1

Jeff Chastine

Page 11: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

16

14 10

9 38 7

2 4 1

Jeff Chastine

Page 12: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

Of Note

• The children’s subtrees each have size at most 2n/3 – when the last row is exactly ½ full

• Therefore, the running time is:

T (n) = T(2n/3) + (1) = O(lg n)

Jeff Chastine

Page 13: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

BUILD-HEAP• Use MAX-HEAPIFY in bottom up manner• Why does the loop start at length[A]/2?• At the start of each loop, each node i is the

root of a max-heap!

BUILD-HEAP (A)heap-size[A] ← length[A]for i ← length[A]/2 downto 1

do MAX-HEAPIFY(A, i)

Jeff Chastine

Page 14: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

Analysis of Building a Heap

• Since each call to MAX-HEAPIFY costs O(lg n) and there are O(n) calls, this is O(n lg n)...

• Can derive a tighter bound: do all nodes take log n time?

• Has at most n/2h+1 nodes at any height (the more the height, the less nodes there are)

• It takes O(h) time to insert a node of height h.

Jeff Chastine

Page 15: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

• Thus, the running time is 2n = O(n)

Sum up the work at each level

n

hh

n

hh

hnhOn lg

0

lg

01 2

)(2

The number ofnodes at height h

Multiplied by their height

Jeff Chastine

Height h islogarithmic

Page 16: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

HEAPSORTHEAPSORT (A)

BUILD-HEAP(A)for i ← length[A] downto 2

do exchange A[1] with A[i]heap-size[A] ← heap-size[A] - 1MAX-HEAPIFY(A, 1)

Jeff Chastine

Page 17: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

16

14 10

9 38 7

2 4 1

Jeff Chastine

Page 18: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

1

14 10

9 38 7

2 4 16

Swap A[1] A[i]Jeff Chastine

Page 19: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

14

8 10

9 34 7

2 1 16

heap-size heap-size – 1MAX-HEAPIFY(A, 1) Jeff Chastine

Page 20: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

1

8 10

9 34 7

2 14 16

Swap A[1] A[i]Jeff Chastine

Page 21: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

10

8 9

1 34 7

2 14 16

heap-size heap-size – 1MAX-HEAPIFY(A, 1) Jeff Chastine

Page 22: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

2

8 9

1 34 7

10 14 16

Swap A[1] A[i]Jeff Chastine

Page 23: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

9

8 3

1 24 7

10 14 16

heap-size heap-size – 1MAX-HEAPIFY(A, 1) Jeff Chastine

Page 24: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

2

8 3

1 94 7

10 14 16

Swap A[1] A[i]Jeff Chastine

Page 25: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

8

7 3

1 94 2

10 14 16

heap-size heap-size – 1MAX-HEAPIFY(A, 1) Jeff Chastine

Page 26: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

1

7 3

8 94 2

10 14 16

Swap A[1] A[i]Jeff Chastine

Page 27: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

7

4 3

8 91 2

10 14 16

heap-size heap-size – 1MAX-HEAPIFY(A, 1) Jeff Chastine

Page 28: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

2

4 3

8 91 7

10 14 16

Swap A[1] A[i]Jeff Chastine

Page 29: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

4

2 3

8 91 7

10 14 16

heap-size heap-size – 1MAX-HEAPIFY(A, 1) Jeff Chastine

Page 30: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

1

2 3

8 94 7

10 14 16

Swap A[1] A[i]Jeff Chastine

Page 31: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

3

2 1

8 94 7

10 14 16

heap-size heap-size – 1MAX-HEAPIFY(A, 1) Jeff Chastine

Page 32: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

1

2 3

8 94 7

10 14 16

Swap A[1] A[i]Jeff Chastine

Page 33: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

2

1 3

8 94 7

10 14 16

heap-size heap-size – 1MAX-HEAPIFY(A, 1) Jeff Chastine

Page 34: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

1

2 3

8 94 7

10 14 16

Swap A[1] A[i]Jeff Chastine

Page 35: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

1

2 3

8 94 7

10 14 16

heap-size heap-size – 1MAX-HEAPIFY(A, 1) Jeff Chastine

Page 36: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

Priority Queues

• A priority queue is a heap that uses a key• Common in operating systems (processes)• Supports HEAP-MAXIMUM, EXTRACT-MAX,

INCREASE-KEY, INSERT

HEAP-MAXIMUM (A) 1 return A[1]

Jeff Chastine

Page 37: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

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

Jeff Chastine

Page 38: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

HEAP-INCREASE-KEY(A, i, key)1 if key < A[i]2 then error “new key smaller than current”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)

Note: runs in O(lg n) Jeff Chastine

Page 39: Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.

MAX-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)

Jeff Chastine