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

Post on 18-Jan-2018

218 views 0 download

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

Heapsort

A minimalist's approach

Jeff Chastine

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

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

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

Simple Functions

PARENT(i)return (i/2)

LEFT(i)return (2i)

RIGHT(i)return (2i + 1)

Jeff Chastine

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

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

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

16

4 10

9 314 7

2 8 1

Jeff Chastine

16

14 10

9 34 7

2 8 1

Jeff Chastine

16

14 10

9 38 7

2 4 1

Jeff Chastine

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

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

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

• 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

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

16

14 10

9 38 7

2 4 1

Jeff Chastine

1

14 10

9 38 7

2 4 16

Swap A[1] A[i]Jeff Chastine

14

8 10

9 34 7

2 1 16

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

1

8 10

9 34 7

2 14 16

Swap A[1] A[i]Jeff Chastine

10

8 9

1 34 7

2 14 16

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

2

8 9

1 34 7

10 14 16

Swap A[1] A[i]Jeff Chastine

9

8 3

1 24 7

10 14 16

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

2

8 3

1 94 7

10 14 16

Swap A[1] A[i]Jeff Chastine

8

7 3

1 94 2

10 14 16

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

1

7 3

8 94 2

10 14 16

Swap A[1] A[i]Jeff Chastine

7

4 3

8 91 2

10 14 16

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

2

4 3

8 91 7

10 14 16

Swap A[1] A[i]Jeff Chastine

4

2 3

8 91 7

10 14 16

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

1

2 3

8 94 7

10 14 16

Swap A[1] A[i]Jeff Chastine

3

2 1

8 94 7

10 14 16

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

1

2 3

8 94 7

10 14 16

Swap A[1] A[i]Jeff Chastine

2

1 3

8 94 7

10 14 16

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

1

2 3

8 94 7

10 14 16

Swap A[1] A[i]Jeff Chastine

1

2 3

8 94 7

10 14 16

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

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

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

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

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