what else can we do with heaps? use the heap for sorting. heapsort!

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

Upload: brent-edmond

Post on 15-Dec-2015

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 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!

Page 2: 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

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

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

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

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

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

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

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

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

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

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!

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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