Warm up Build a Max Heap from the following Elements: 4, 15, 22, 6, 18, 30, 14, 21
CS4102 Algorithms
Fall 2018
1
CS4102 Algorithms Fall 2018 Warm up Build a Max Heap from the - - PowerPoint PPT Presentation
CS4102 Algorithms Fall 2018 Warm up Build a Max Heap from the following Elements: 4, 15, 22, 6, 18, 30, 14, 21 1 Heap Heap Property: Each node must be larger than its children 30 1 22 21 3 2 4 14 19 15 6 7 4 5 6 30 21 22
1
2
30 21 22 19 15 4 14 6 1 2 3 4 6 5 7 8 30 21 22 19 15 4 14 1 2 3 4 5 6 7 6 8
3
4
5
6
>or<? >or<? >or<? >or<? >or<? >or<? >or<? >or<? >or<? >or<? >or<? >or<? >or<? >or<? >or<? [1,2,3,4,5] [2,1,3,4,5] [5,2,4,1,3] [5,4,3,2,1]
… … … … … … > < < > < > > > > < < < < > One comparison Result of comparison Permutation
Possible execution path 𝑜! Possible permutations
log 𝑜! Θ(𝑜 log 𝑜)
7
8
– Break 𝑜-element list into two lists of Τ
𝑜 2 elements
– If 𝑜 > 1: Sort each sublist recursively – If 𝑜 = 1: List is already sorted (base case)
– Merge together sorted sublists into one sorted list
10
– Break 𝑜-element list into two lists of Τ
𝑜 2 elements
– If 𝑜 > 1: Sort each sublist recursively – If 𝑜 = 1: List is already sorted (base case)
– Merge together sorted sublists into one sorted list
11
𝑜 2 elements
12
𝑜 𝑜 2 𝑜 2 𝑜 4 𝑜 4 𝑜 4 𝑜 4 1 1 1 1 1 1
𝑜 𝑜 2 𝑜 2 𝑜 4 𝑜 4 𝑜 4 𝑜 4 1 1 1 1 1 1
𝑜 2 𝑜 4 1
two sublists around that element
16
8 5 7 9 12 10 1 2 4 3 6 11 5 8 7 9 12 10 1 2 4 3 6 11 5 7 8 9 12 10 1 2 4 3 6 11 5 7 8 9 12 10 1 2 4 3 6 11
elements if out of order, repeat until sorted
18
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
2 3 4 5 6 7 8 9 10 11 12 1
2 3 4 5 6 7 8 9 10 11 1 12
elements if out of order, repeat until sorted
20
3 5 7 8 10 12 9 2 4 6 1 11 Sorted Prefix 3 5 7 8 10 9 12 2 4 6 1 11 3 5 7 8 9 10 12 2 4 6 1 11 3 5 7 8 9 10 12 2 4 6 1 11 Sorted Prefix
prefix by “inserting” the next element
22
1 2 3 4 5 6 7 8 9 10 11 12 Sorted Prefix 1 2 3 4 5 6 7 8 9 10 11 12 Sorted Prefix
prefix by “inserting” the next element
24
3 5 7 8 10 12 10’ 2 4 6 1 11 Sorted Prefix 3 5 7 8 10 10’ 12 2 4 6 1 11 3 5 7 8 10 10’ 12 2 4 6 1 11 Sorted Prefix
prefix by “inserting” the next element
26
10 9 6 8 7 5 2 4 1 3 10 9 6 8 7 5 2 4 1 3
1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9 10
27
3 9 6 8 7 5 2 4 1 3 9 6 8 7 5 2 4 1
1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9
28
9 3 6 8 7 5 2 4 1 9 3 6 8 7 5 2 4 1
1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9
29
9 8 6 3 7 5 2 4 1 9 8 6 3 7 5 2 4 1
1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9
30
9 8 6 4 7 5 2 3 1 9 8 6 4 7 5 2 3 1
1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9
element from the heap to build sorted list Right- to-Left
32
10 9 6 8 7 5 2 4 1 3 10 9 6 8 7 5 2 4 1 3
1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9 10
33
3 9 6 8 7 5 2 4 1 3 9 6 8 7 5 2 4 1 10
1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9
34
9 8 6 4 7 5 2 3 1 9 8 6 4 7 5 2 3 1 10
1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9
35
8 7 6 4 1 5 2 3 8 7 6 4 1 5 2 3 9 10
1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8
36
7 4 6 3 1 5 2 7 4 6 3 1 5 2 8 9 10
1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7
element from the heap to build sorted list Right- to-Left
38
3 6 6 1 3 4 1 6 1 2 3 4 5 6 7 8 2 2 1 3 1 2 3 4 5 6
2 2 4 5 5 8 1 2 3 4 5 6
running sum To sort: last item of value 3 goes at index 4
39
3 6 6 1 3 4 1 6 1 2 3 4 5 6 7 8
2 2 4 5 5 8 1 2 3 4 5 6
Last item of value 6 goes at index 8
1 2 3 4 5 6 7 8
40
7 6
3 6 6 1 3 4 1 6 1 2 3 4 5 6 7 8
2 2 4 5 5 7 1 2 3 4 5 6
Last item of value 1 goes at index 2
6 1 2 3 4 5 6 7 8
41
1 1
42
43
44
103 801 401 323 255 823 999 101 1 2 3 4 5 6 7
999 018 255 555 245 103 323 823 113 512 113 901 555 512 245 800 018 121 8 9 10 11 12 13 14 15 1 2 3 4 5 6 7 801 401 101 901 121 800 8 9
45
999 018 255 555 245 103 323 823 113 512 1 2 3 4 5 6 7 801 401 101 901 121 800 8 9 999 255 555 245 121 323 823 1 2 3 4 5 6 7 512 113 018 800 801 401 101 901 103 8 9
46
999 255 555 245 121 323 823 1 2 3 4 5 6 7 512 113 018 800 801 401 101 901 103 8 9 901 999 800 801 823 512 555 401 323 245 255 1 2 3 4 5 6 7 101 103 113 121 018 8 9
47
2 5 3 2 3 5 1 2 3 4 5 6 7 2 2 3 1 1 2 3 4 5
2 2 3 7 7 8 1 2 3 4 5
48
2 5 3 2 3 5 1 2 3 4 5 6 7
2 2 3 7 7 8 1 2 3 4 5