What else can we do with heaps? Use the heap for sorting. Heapsort!

Post on 15-Dec-2015

223 views 1 download

Transcript of What else can we do with heaps? Use the heap for sorting. Heapsort!

What else can we do with heaps?

Use the heap for sorting.

Heapsort!

Basic Idea

• Given an unsorted array we use BuildHeap to convert it into a heap

• While(heap is not empty)– removeMin– The heap is one smaller but the array hasn't changed– Put the item just removed in the element just after the end of

the heap

• At conclusion the array is sorted

HeapSortafter BuildHeap

1

5 2

5 6 3 9

11 19 7 12 23 22 68 14

87 43 27 35 99 42 77 71 33 67 89

size = 26

HeapSort

1

5 2

5 6 3 9

11 19 7 12 23 22 68 14

87 43 27 35 99 42 77 71 33 67 89

size = 26removeMin

HeapSort

2

5 3

5 6 22 9

11 19 7 12 23 89 68 14

87 43 27 35 99 42 77 71 33 67

size = 25removeMin = 1

Not in heap now

HeapSort

2

5 3

5 6 22 9

11 19 7 12 23 89 68 14

87 43 27 35 99 42 77 71 33 67 1

size = 25

heap is 1 element smaller, smallest element is at end of array

HeapSort

2

5 3

5 6 22 9

11 19 7 12 23 89 68 14

87 43 27 35 99 42 77 71 33 67 1

size = 25

Now do it again!

HeapSort

3

5 9

5 6 22 14

11 19 7 12 23 89 68 67

87 43 27 35 99 42 77 71 33 2 1

size = 24

HeapSort

5

5 9

11 6 22 14

33 19 7 12 23 89 68 67

87 43 27 35 99 42 77 71 3 2 1

size = 23

HeapSort

5

6 9

11 7 22 14

33 19 42 12 23 89 68 67

87 43 27 35 99 71 77 5 3 2 1

size = 22

HeapSort

6

7 9

11 7 22 14

33 19 42 12 23 89 68 67

87 43 27 35 99 71 5 5 3 2 1

size = 21

HeapSort

7

7 9

11 12 22 14

33 19 42 71 23 89 68 67

87 43 27 35 99 6 5 5 3 2 1

size = 20

HeapSort

7

11 9

19 12 22 14

33 27 42 71 23 89 68 67

87 43 99 35 7 6 5 5 3 2 1

size = 19

HeapSort

9

11 14

19 12 22 35

33 27 42 71 23 89 68 67

87 43 99 7 7 6 5 5 3 2 1

size = 18

HeapSort

11

12 14

19 42 22 35

33 27 99 71 23 89 68 67

87 43 9 7 7 6 5 5 3 2 1

size = 17

HeapSort

12

19 14

27 42 22 35

33 43 99 71 23 89 68 67

87 11 9 7 7 6 5 5 3 2 1

size = 16

HeapSort

14

19 22

27 42 23 35

33 43 99 71 87 89 68 67

12 11 9 7 7 6 5 5 3 2 1

size = 15

HeapSort

19

27 22

33 42 23 35

67 43 99 71 87 89 68 14

12 11 9 7 7 6 5 5 3 2 1

size = 14

HeapSort

22

27 23

33 42 68 35

67 43 99 71 87 89 19 14

12 11 9 7 7 6 5 5 3 2 1

size = 13

HeapSort

23

27 35

33 42 68 89

67 43 99 71 87 22 19 14

12 11 9 7 7 6 5 5 3 2 1

size = 12

HeapSort

27

33 35

43 42 68 89

67 87 99 71 23 22 19 14

12 11 9 7 7 6 5 5 3 2 1

size = 11

HeapSort

33

42 35

43 71 68 89

67 87 99 27 23 22 19 14

12 11 9 7 7 6 5 5 3 2 1

size = 10

HeapSort

35

42 68

43 71 99 89

67 87 33 27 23 22 19 14

12 11 9 7 7 6 5 5 3 2 1

size = 9

HeapSort

42

43 68

67 71 99 89

87 35 33 27 23 22 19 14

12 11 9 7 7 6 5 5 3 2 1

size = 8

HeapSort

43

67 68

87 71 99 89

42 35 33 27 23 22 19 14

12 11 9 7 7 6 5 5 3 2 1

size = 7

HeapSort

67

71 68

87 89 99 43

42 35 33 27 23 22 19 14

12 11 9 7 7 6 5 5 3 2 1

size = 6

HeapSort

68

71 99

87 89 67 43

42 35 33 27 23 22 19 14

12 11 9 7 7 6 5 5 3 2 1

size = 5

HeapSort

71

87 99

89 68 67 43

42 35 33 27 23 22 19 14

12 11 9 7 7 6 5 5 3 2 1

size = 4

HeapSort

87

89 99

71 68 67 43

42 35 33 27 23 22 19 14

12 11 9 7 7 6 5 5 3 2 1

size = 3

HeapSort

89

99 87

71 68 67 43

42 35 33 27 23 22 19 14

12 11 9 7 7 6 5 5 3 2 1

size = 2

HeapSort

99

89 87

71 68 67 43

42 35 33 27 23 22 19 14

12 11 9 7 7 6 5 5 3 2 1

size = 1