Warm up Show log π! = Ξ(π log π) Hint: show π! β€ ππ Hint 2: show π! β₯
π 2
π 2
1
CS4102 Algorithms Summer 2020 Warm up Show log ! = ( log ) Hint: - - PowerPoint PPT Presentation
CS4102 Algorithms Summer 2020 Warm up Show log ! = ( log ) Hint: show ! 2 Hint 2: show ! 2 1 log ! = log ! = 1 2 2 1 = <
π 2
1
2
3
π 2 = π
π 2
π 2
4
5
Master theorem Case 3! Because π π = Ξ©(π)
6
2 5 1 3 6 4 7 8 10 9 11 12 2 1 3 5 6 4 7 8 9 10 11 12
7
8
10
th order statistic:
π π/10 9π/10 π/100 9π/100 9π/100 81π/100 1 1 1 1
π π π + + + + + + +
9
10
10
th order statistic:
11
1 5 2 3 6 4 7 8 10 9 11 12 1 2 3 5 6 4 7 8 10 9 11 12
12
π π
π πβπ
13
14
1 2 3 4 5 6 7 8 9 10 11 12
15
1 2 3 4 5 6 7 8 9 10 11 12
12
first pivot
1 πβπ+1
1 πβπ+1 π<π
16
1 π β π + 1
π<π
17
2 2
1 2 3 4 5 6 7 8 9 10 11 12
1 π β π + 1
π<π
18
2 2 + 2 3
1 2 3 4 5 6 7 8 9 10 11 12
1 π β π + 1
π<π
19
2 2 + 2 3 + 2 4
1 2 3 4 5 6 7 8 9 10 11 12
1 π β π + 1
π<π
20
2 2 + 2 3 + 2 4 + 2 5 + β― + 2 π
1 2 3 4 5 6 7 8 9 10 11 12
1 π β π + 1
π<π
21
When π = 1: 2
1 2 + 1 3 + 1 4 + β― + 1 π
π terms overall 1 π β π + 1
π<π
β€ 2π 1 2 + 1 3 + β― + 1 π
22
π(π log π) π(π log π) π(π log π) π(π2) π(π2) Can we do better than π(π log π)?
23
24
>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
25
>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 π)
β There is no (comparison-based) sorting algorithm with run time π(π log π)
26
>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 π)
27
π(π log π) π(π log π) π(π log π) π(π2) π(π2) Optimal! Optimal! Optimal!
28
β 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
β 2 sorted lists (π1, π2) β 1 output list (πππ£π’)
Stable: If elements are equal, leftmost comes first
30
β 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
31
32
Parallelizable: Allow different machines to work
π π = 2π(π 2 ) + π
π π 2 π 2 π 4 π 4 π 4 π 4 1 1 1 1 1 1
π π = π(π 2 ) + π
π π 2 π 2 π 4 π 4 π 4 π 4 1 1 1 1 1 1
π 2 π 4 1
two sublists around that element
36
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
38
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
Only makes one βpassβ
2 3 4 5 6 7 8 9 10 11 12 1
2 3 4 5 6 7 8 9 10 11 1 12
Requires π passes, thus is π(π2)
elements if out of order, repeat until sorted
"the bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problemsβ βDonald Knuth, The Art of Computer Programming
40
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
(but with very small constants) Great for short lists!
prefix by βinsertingβ the next element
42
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
(but with very small constants) Great for short lists!
44
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
The βsecondβ 10 will stay to the right
prefix by βinsertingβ the next element
βAll things considered, itβs actually a pretty good sorting algorithm!β βNate Brunelle
(but with very small constants) Great for short lists!
46
10 9 6 8 7 5 2 4 1 3 10 9 6 8 7 5 2 4 1 3
Max Heap Property: Each node is larger than its children
1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9 10
47
3 9 6 8 7 5 2 4 1 3 9 6 8 7 5 2 4 1
Max Heap Property: Each node is larger than its children
1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9
48
9 3 6 8 7 5 2 4 1 9 3 6 8 7 5 2 4 1
Max Heap Property: Each node is larger than its children
1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9
49
9 8 6 3 7 5 2 4 1 9 8 6 3 7 5 2 4 1
Max Heap Property: Each node is larger than its children
1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9
50
9 8 6 4 7 5 2 3 1 9 8 6 4 7 5 2 3 1
Max Heap Property: Each node is larger than its children
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
When removing an element from the heap, move it to the (now unoccupied) end of the list
52
10 9 6 8 7 5 2 4 1 3 10 9 6 8 7 5 2 4 1 3
Max Heap Property: Each node is larger than its children
1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9 10
53
3 9 6 8 7 5 2 4 1 3 9 6 8 7 5 2 4 1 10
Max Heap Property: Each node is larger than its children
1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9
54
9 8 6 4 7 5 2 3 1 9 8 6 4 7 5 2 3 1 10
Max Heap Property: Each node is larger than its children
1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8 9
55
8 7 6 4 1 5 2 3 8 7 6 4 1 5 2 3 9 10
Max Heap Property: Each node is larger than its children
1 2 3 4 5 6 7 8 9 10 1 2 3 4 6 5 7 8
56
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
58
59
Range is [1, π] (here [1,6]) make an array π· of size π populate with counts of each value
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
π· = For π in π: + +C π π 1. π = Take βrunning sumβ of π· to count things less than each value
2 2 4 5 5 8 1 2 3 4 5 6
π· = For π = 1 to len(π·): π· π = π· π β 1 + π·[π] 2.
running sum To sort: last item of value 3 goes at index 4
60
3 6 6 1 3 4 1 6 1 2 3 4 5 6 7 8
π = For each element of π (last to first): Use π· to find its proper place in πΆ Decrement that position of C
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
7 6
61
3 6 6 1 3 4 1 6 1 2 3 4 5 6 7 8
π = For each element of π (last to first): Use π· to find its proper place in πΆ Decrement that position of C
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
1 1
Run Time: π π + π Memory: π π + π
62
One Exabyte = 1018 bytes 1 million terabytes (TB) 1 billion gigabytes (GB) 100,000 x Library of Congress (print)
63 https://en.wikipedia.org/wiki/Utah_Data_Center
64
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
65
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
66
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