CS4102 Algorithms Summer 2020 Warm up Show log ! = ( log ) Hint: - - PowerPoint PPT Presentation

β–Ά
cs4102 algorithms
SMART_READER_LITE
LIVE PREVIEW

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 = <


slide-1
SLIDE 1

Warm up Show log π‘œ! = Θ(π‘œ log π‘œ) Hint: show π‘œ! ≀ π‘œπ‘œ Hint 2: show π‘œ! β‰₯

π‘œ 2

π‘œ 2

1

CS4102 Algorithms

Summer 2020

slide-2
SLIDE 2

2

log π‘œ! = 𝑃 π‘œ log π‘œ

π‘œ! = π‘œ β‹… π‘œ βˆ’ 1 β‹… π‘œ βˆ’ 2 β‹… … β‹… 2 β‹… 1 π‘œπ‘œ = π‘œ β‹… π‘œ β‹… π‘œ β‹… … β‹… π‘œ β‹… π‘œ = < < < < π‘œ! ≀ π‘œπ‘œ β‡’ log π‘œ! ≀ log π‘œπ‘œ β‡’ log π‘œ! ≀ π‘œ log π‘œ β‡’ log π‘œ! = 𝑃(π‘œ log π‘œ)

slide-3
SLIDE 3

3

log π‘œ! = Ξ© π‘œ log π‘œ

π‘œ! = π‘œ β‹… π‘œ βˆ’ 1 β‹… π‘œ βˆ’ 2 β‹… … β‹… π‘œ 2 β‹… π‘œ 2 βˆ’ 1 β‹… … β‹… 2 β‹… 1 π‘œ 2

π‘œ 2 = π‘œ

2 β‹… π‘œ 2 β‹… π‘œ 2 β‹… … β‹… π‘œ 2 β‹… 1 β‹… … β‹… 1 β‹… 1 > > > > = π‘œ! β‰₯ π‘œ 2

π‘œ 2

β‡’ log π‘œ! β‰₯ log π‘œ 2

π‘œ 2

β‡’ log π‘œ! β‰₯ π‘œ 2 log π‘œ 2 β‡’ log π‘œ! = Ξ©(π‘œ log π‘œ) > =

slide-4
SLIDE 4

Median of Medians, Run Time

4

  • 1. Break list into chunks of 5
  • 2. Find the median of each chunk
  • 3. Return median of medians (using Quickselect)

Θ(π‘œ) Θ(π‘œ) 𝑇 π‘œ 5 𝑁 π‘œ = 𝑇 π‘œ 5 + Θ(π‘œ)

slide-5
SLIDE 5

Quickselect

5

𝑁 π‘œ = 𝑇 π‘œ 5 + Θ(π‘œ) 𝑇 π‘œ ≀ 𝑇 7π‘œ 10 + 𝑁 π‘œ + Θ(π‘œ) = 𝑇 7π‘œ 10 + 𝑇 π‘œ 5 + Θ(π‘œ) 𝑇 π‘œ = O(π‘œ) = 𝑇 7π‘œ 10 + 𝑇 2π‘œ 10 + Θ(π‘œ) ≀ 𝑇 9π‘œ 10 + Θ(π‘œ)

Master theorem Case 3! Because 𝑇 π‘œ = Ξ©(π‘œ)

𝑇 π‘œ = Θ(π‘œ)

slide-6
SLIDE 6

Phew! Back to Quicksort

Then we divide in half each time

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

π‘ˆ π‘œ = 2π‘ˆ π‘œ 2 + Θ(π‘œ)

Using Quickselect, with a median-of-medians partition:

π‘ˆ π‘œ = Θ(π‘œ log π‘œ)

slide-7
SLIDE 7

Random Pivot

  • Using Quickselect to pick median guarantees Θ(π‘œ log π‘œ) run

time

– Approach has very large constants – If you really want Θ(π‘œ log π‘œ), better off using MergeSort

  • Better approach: Random pivot

– Very small constant (very fast algorithm) – Expected to run in Θ(π‘œ log π‘œ) time

  • Why? Unbalanced partitions are very unlikely

– Other options: Median of 5

7

slide-8
SLIDE 8

Quicksort Run Time

8

π‘ˆ π‘œ = π‘ˆ π‘œ 10 + π‘ˆ 9π‘œ 10 + π‘œ

If the pivot is always π‘œ

10

th order statistic:

slide-9
SLIDE 9

π‘œ

π‘ˆ π‘œ = π‘ˆ π‘œ 10 + π‘ˆ 9π‘œ 10 + π‘œ

π‘œ 10 9π‘œ 10 π‘œ 100 9π‘œ 100 9π‘œ 100 81π‘œ 100

… … … …

1 1 1 1

π‘œ π‘œ/10 9π‘œ/10 π‘œ/100 9π‘œ/100 9π‘œ/100 81π‘œ/100 1 1 1 1

π‘œ π‘œ π‘œ + + + + + + +

log 10

9

π‘œ

slide-10
SLIDE 10

Quicksort Run Time

10

π‘ˆ π‘œ = π‘ˆ π‘œ 10 + π‘ˆ 9π‘œ 10 + π‘œ

If the pivot is always π‘œ

10

th order statistic:

π‘ˆ π‘œ = Θ(π‘œ log π‘œ)

slide-11
SLIDE 11

Quicksort Run Time

Then we shorten by 𝑒 each time

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

π‘ˆ π‘œ = π‘ˆ π‘œ βˆ’ 𝑒 + π‘œ

If the pivot is always 𝑒th order statistic:

π‘ˆ π‘œ = 𝑃(π‘œ2) What’s the probability of this occurring?

slide-12
SLIDE 12

Probability of π‘œ2 run time

We must consistently select pivot from within the first 𝑒 terms

12

Probability first pivot is among 𝑒 smallest:

𝑒 π‘œ

Probability second pivot is among 𝑒 smallest:

𝑒 π‘œβˆ’π‘’

Probability all pivots are among 𝑒 smallest: 𝑒 π‘œ β‹… 𝑒 π‘œ βˆ’ 𝑒 β‹… 𝑒 π‘œ βˆ’ 2𝑒 β‹… … β‹… 𝑒 2𝑒 β‹… 1 = 1 π‘œ 𝑒 !

slide-13
SLIDE 13

Formal Argument for π‘œ log π‘œ Average

  • Remember, run time counts comparisons!
  • Quicksort only compares against a pivot

– Element 𝑗 only compared to element π‘˜ if one of them was the pivot

13

slide-14
SLIDE 14

Formal Argument for π‘œ log π‘œ Average

  • What is the probability of comparing two

given elements?

14

1 2 3 4 5 6 7 8 9 10 11 12

  • (Probability of comparing 3 and 4) = 1

– Why? Otherwise I wouldn’t know which came first – ANY sorting algorithm must compare adjacent elements

slide-15
SLIDE 15

Formal Argument for π‘œ log π‘œ Average

  • What is the probability of comparing two

given elements?

15

1 2 3 4 5 6 7 8 9 10 11 12

  • (Probability of comparing 1 and 12) = 2

12

– Why?

  • I only compare 1 with 12 if either was chosen as the

first pivot

  • Otherwise they would be divided into opposite sublists
slide-16
SLIDE 16

Formal Argument for π‘œ log π‘œ Average

  • Probability of comparing 𝑗 with π‘˜ (π‘˜ > 𝑗):

– dependent on the number of elements between 𝑗 and π‘˜ –

1 π‘˜βˆ’π‘—+1

  • Expected number of comparisons:

–

1 π‘˜βˆ’π‘—+1 𝑗<π‘˜

16

slide-17
SLIDE 17

Expected number of Comparisons

1 π‘˜ βˆ’ 𝑗 + 1

𝑗<π‘˜

17

Consider when 𝑗 = 1 Sum so far:

2 2

1 2 3 4 5 6 7 8 9 10 11 12

Compared if 1 or 2 are chosen as pivot (these will always be compared)

slide-18
SLIDE 18

Expected number of Comparisons

1 π‘˜ βˆ’ 𝑗 + 1

𝑗<π‘˜

18

Consider when 𝑗 = 1 Sum so far:

2 2 + 2 3

1 2 3 4 5 6 7 8 9 10 11 12

Compared if 1 or 3 are chosen as pivot (but never if 2 is ever chosen)

slide-19
SLIDE 19

Expected number of Comparisons

1 π‘˜ βˆ’ 𝑗 + 1

𝑗<π‘˜

19

Consider when 𝑗 = 1 Sum so far:

2 2 + 2 3 + 2 4

1 2 3 4 5 6 7 8 9 10 11 12

Compared if 1 or 4 are chosen as pivot (but never if 2 or 3 are chosen)

slide-20
SLIDE 20

Expected number of Comparisons

1 π‘˜ βˆ’ 𝑗 + 1

𝑗<π‘˜

20

Consider when 𝑗 = 1 Overall sum:

2 2 + 2 3 + 2 4 + 2 5 + β‹― + 2 π‘œ

1 2 3 4 5 6 7 8 9 10 11 12

Compared if 1 or 12 are chosen as pivot (but never if 2 -> 11 are chosen)

slide-21
SLIDE 21

Expected number of Comparisons

1 π‘˜ βˆ’ 𝑗 + 1

𝑗<π‘˜

21

When 𝑗 = 1: 2

1 2 + 1 3 + 1 4 + β‹― + 1 π‘œ

π‘œ terms overall 1 π‘˜ βˆ’ 𝑗 + 1

𝑗<π‘˜

≀ 2π‘œ 1 2 + 1 3 + β‹― + 1 π‘œ

Θ(log π‘œ) Quicksort overall: expected Θ π‘œ log π‘œ

slide-22
SLIDE 22

Sorting, so far

  • Sorting algorithms we have discussed:

– Mergesort – Quicksort

  • Other sorting algorithms (will discuss):

– Bubblesort – Insertionsort – Heapsort

22

𝑃(π‘œ log π‘œ) 𝑃(π‘œ log π‘œ) 𝑃(π‘œ log π‘œ) 𝑃(π‘œ2) 𝑃(π‘œ2) Can we do better than 𝑃(π‘œ log π‘œ)?

slide-23
SLIDE 23

Worst Case Lower Bounds

  • Prove that there is no algorithm which can sort faster than

𝑃(π‘œ log π‘œ)

– Every algorithm, in the worst case, must have a certain lower bound

  • Non-existence proof!

– Very hard to do

23

slide-24
SLIDE 24

Strategy: Decision Tree

  • Sorting algorithms use comparisons to figure out the order of input

elements

  • Draw tree to illustrate all possible execution paths

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

  • f sorted list

Possible execution path

slide-25
SLIDE 25

Strategy: Decision Tree

  • Worst case run time is the longest execution path
  • i.e., β€œheight” of the decision tree

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

  • f sorted list

Possible execution path π‘œ! Possible permutations

log π‘œ! Θ(π‘œ log π‘œ)

slide-26
SLIDE 26

Strategy: Decision Tree

  • Conclusion: Worst Case Optimal run time of sorting is Θ(π‘œ 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

  • f sorted list

Possible execution path π‘œ! Possible permutations

log π‘œ! Θ(π‘œ log π‘œ)

slide-27
SLIDE 27

Sorting, so far

  • Sorting algorithms we have discussed:

– Mergesort – Quicksort

  • Other sorting algorithms (will discuss):

– Bubblesort – Insertionsort – Heapsort

27

𝑃(π‘œ log π‘œ) 𝑃(π‘œ log π‘œ) 𝑃(π‘œ log π‘œ) 𝑃(π‘œ2) 𝑃(π‘œ2) Optimal! Optimal! Optimal!

slide-28
SLIDE 28

Speed Isn’t Everything

  • Important properties of sorting algorithms:
  • Run Time

– Asymptotic Complexity – Constants

  • In Place (or In-Situ)

– Done with only constant additional space

  • Adaptive

– Faster if list is nearly sorted

  • Stable

– Equal elements remain in original order

  • Parallelizable

– Runs faster with multiple computers

28

slide-29
SLIDE 29

Mergesort

  • Divide:

– Break π‘œ-element list into two lists of π‘œ 2 elements

  • Conquer:

– If π‘œ > 1: Sort each sublist recursively – If π‘œ = 1: List is already sorted (base case)

  • Combine:

– Merge together sorted sublists into one sorted list

Run Time? Θ(π‘œ log π‘œ) Optimal! In Place? Adaptive? Stable? No No Yes! (usually)

slide-30
SLIDE 30

Merge

  • Combine: Merge sorted sublists into one sorted list
  • We have:

– 2 sorted lists (𝑀1, 𝑀2) – 1 output list (𝑀𝑝𝑣𝑒)

While (𝑀1 and 𝑀2 not empty): If 𝑀1 0 ≀ 𝑀2[0]: 𝑀𝑝𝑣𝑒.append(𝑀1.pop()) Else: 𝑀𝑝𝑣𝑒.append(𝑀2.pop()) 𝑀𝑝𝑣𝑒.append(𝑀1) 𝑀𝑝𝑣𝑒.append(𝑀2)

Stable: If elements are equal, leftmost comes first

30

slide-31
SLIDE 31

Mergesort

  • Divide:

– Break π‘œ-element list into two lists of π‘œ 2 elements

  • Conquer:

– If π‘œ > 1: Sort each sublist recursively – If π‘œ = 1: List is already sorted (base case)

  • Combine:

– Merge together sorted sublists into one sorted list

Run Time? Θ(π‘œ log π‘œ) Optimal! In Place? Adaptive? Stable? Parallelizable? No No Yes! (usually) Yes!

31

slide-32
SLIDE 32

Mergesort

  • Divide:

– Break π‘œ-element list into two lists of π‘œ 2 elements

  • Conquer:

– If π‘œ > 1:

  • Sort each sublist recursively

– If π‘œ = 1:

  • List is already sorted (base case)
  • Combine:

– Merge together sorted sublists into one sorted list

32

Parallelizable: Allow different machines to work

  • n each sublist
slide-33
SLIDE 33

Mergesort (Sequential)

π‘œ total / level log2 π‘œ levels

  • f recursion

π‘œ

π‘ˆ π‘œ = 2π‘ˆ(π‘œ 2 ) + π‘œ

π‘œ 2 π‘œ 2 π‘œ 4 π‘œ 4 π‘œ 4 π‘œ 4

… … … …

1 1 1

…

1 1 1

π‘œ π‘œ 2 π‘œ 2 π‘œ 4 π‘œ 4 π‘œ 4 π‘œ 4 1 1 1 1 1 1

Run Time: Θ(π‘œ log π‘œ)

slide-34
SLIDE 34

Mergesort (Parallel)

π‘œ

π‘ˆ π‘œ = π‘ˆ(π‘œ 2 ) + π‘œ

π‘œ 2 π‘œ 2 π‘œ 4 π‘œ 4 π‘œ 4 π‘œ 4

… … … …

1 1 1

…

1 1 1

π‘œ π‘œ 2 π‘œ 2 π‘œ 4 π‘œ 4 π‘œ 4 π‘œ 4 1 1 1 1 1 1

Run Time: Θ(π‘œ) Done in Parallel

π‘œ 2 π‘œ 4 1

slide-35
SLIDE 35

Quicksort

Run Time? Θ(π‘œ log π‘œ)

(almost always) Better constants than Mergesort

In Place? Adaptive? Stable? kinda No! No Parallelizable? Yes!

  • Idea: pick a partition element, recursively sort

two sublists around that element

  • Divide: select an element π‘ž, Partition(π‘ž)
  • Conquer: recursively sort left and right sublists
  • Combine: Nothing!

Uses stack for recursive calls

slide-36
SLIDE 36

Bubble Sort

  • Idea: March through list, swapping adjacent

elements if out of order, repeat until sorted

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

slide-37
SLIDE 37

Bubble Sort

Run Time? Θ(π‘œ2)

Constants worse than Insertion Sort

In Place? Adaptive? Yes Kinda

  • Idea: March through list, swapping adjacent

elements if out of order, repeat until sorted

β€œCompared to straight insertion […], bubble sorting requires a more complicated program and takes about twice as long!” –Donald Knuth

slide-38
SLIDE 38

Bubble Sort is β€œalmost” Adaptive

  • Idea: March through list, swapping adjacent

elements if out of order

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

After one β€œpass”

2 3 4 5 6 7 8 9 10 11 1 12

Requires π‘œ passes, thus is 𝑃(π‘œ2)

slide-39
SLIDE 39

Bubble Sort

Run Time? Θ(π‘œ2)

Constants worse than Insertion Sort

In Place? Adaptive? Stable? Yes! Kinda Not really Yes Parallelizable? No

  • Idea: March through list, swapping adjacent

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

slide-40
SLIDE 40

Insertion Sort

  • Idea: Maintain a sorted list prefix, extend that

prefix by β€œinserting” the next element

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

slide-41
SLIDE 41

Insertion Sort

Run Time? Θ(π‘œ2)

(but with very small constants) Great for short lists!

In Place? Adaptive? Yes! Yes

  • Idea: Maintain a sorted list prefix, extend that

prefix by β€œinserting” the next element

slide-42
SLIDE 42

Insertion Sort is Adaptive

  • Idea: Maintain a sorted list prefix, extend that

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

Only one comparison needed per element! Runtime: 𝑃(π‘œ)

slide-43
SLIDE 43

Insertion Sort

Run Time? In Place? Adaptive? Stable? Yes! Yes Yes

  • Idea: Maintain a sorted list prefix, extend that

prefix by β€œinserting” the next element

Θ(π‘œ2)

(but with very small constants) Great for short lists!

slide-44
SLIDE 44

Insertion Sort is Stable

  • Idea: Maintain a sorted list prefix, extend that

prefix by β€œinserting” the next element

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

slide-45
SLIDE 45

Insertion Sort

Run Time? In Place? Adaptive? Stable? Yes! Yes Yes Parallelizable? No

  • Idea: Maintain a sorted list prefix, extend that

prefix by β€œinserting” the next element

Online? Yes

Can sort a list as it is received, i.e., don’t need the entire list to begin sorting

β€œAll things considered, it’s actually a pretty good sorting algorithm!” –Nate Brunelle

Θ(π‘œ2)

(but with very small constants) Great for short lists!

slide-46
SLIDE 46

Heap Sort

  • Idea: Build a Heap, repeatedly extract max element

from the heap to build sorted list Right-to-Left

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

slide-47
SLIDE 47

Heap Sort

  • Remove the Max element (i.e. the root) from the

Heap: replace with last element, call Heapify(root)

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

Heapify(node): if node satisfies heap property, done. Else swap with largest child and recurse on that subtree

slide-48
SLIDE 48

Heap Sort

  • Remove the Max element (i.e. the root) from the

Heap: replace with last element, call Heapify(root)

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

Heapify(node): if node satisfies heap property, done. Else swap with largest child and recurse on that subtree

slide-49
SLIDE 49

Heap Sort

  • Remove the Max element (i.e. the root) from the

Heap: replace with last element, call Heapify(root)

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

Heapify(node): if node satisfies heap property, done. Else swap with largest child and recurse on that subtree

slide-50
SLIDE 50

Heap Sort

  • Remove the Max element (i.e. the root) from the

Heap: replace with last element, call Heapify(root)

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

Heapify(node): if node satisfies heap property, done. Else swap with largest child and recurse on that subtree

slide-51
SLIDE 51

Heap Sort

Run Time? Θ(π‘œ log π‘œ)

Constants worse than Quick Sort

In Place? Yes!

  • Idea: Build a Heap, repeatedly extract max

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

slide-52
SLIDE 52

In Place Heap Sort

  • Idea: 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

slide-53
SLIDE 53

In Place Heap Sort

  • Idea: When removing an element from the heap,

move it to the (now unoccupied) end of the list

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

slide-54
SLIDE 54

In Place Heap Sort

  • Idea: When removing an element from the heap,

move it to the (now unoccupied) end of the list

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

slide-55
SLIDE 55

In Place Heap Sort

  • Idea: When removing an element from the heap,

move it to the (now unoccupied) end of the list

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

slide-56
SLIDE 56

In Place Heap Sort

  • Idea: When removing an element from the heap,

move it to the (now unoccupied) end of the list

56

7 4 6 3 1 5 2 7 4 6 3 1 5 2 8 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

slide-57
SLIDE 57

Heap Sort

Run Time? Θ(π‘œ log π‘œ)

Constants worse than Quick Sort

In Place? Adaptive? Stable? Yes! No No Parallelizable? No

  • Idea: Build a Heap, repeatedly extract max

element from the heap to build sorted list Right- to-Left

slide-58
SLIDE 58

Sorting in Linear Time

  • Cannot be comparison-based
  • Need to make some sort of assumption about the contents of

the list

– Small number of unique values – Small range of values – Etc.

58

slide-59
SLIDE 59

Counting Sort

59

  • Idea: Count how many things are less than each element

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

slide-60
SLIDE 60

Counting Sort

60

  • Idea: Count how many things are less than each element

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

𝐢 = For 𝑗 = len(𝑀) downto 1: 𝐢 𝐷 𝑀 𝑗 = 𝑀 𝑗 𝐷 𝑀 𝑗 = 𝐷 𝑀 𝑗 βˆ’ 1

7 6

slide-61
SLIDE 61

Counting Sort

61

  • Idea: Count how many things are less than each element

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

𝐢 = For 𝑗 = len(𝑀) downto 1: 𝐢 𝐷 𝑀 𝑗 = 𝑀 𝑗 𝐷 𝑀 𝑗 = 𝐷 𝑀 𝑗 βˆ’ 1

1 1

Run Time: 𝑃 π‘œ + 𝑙 Memory: 𝑃 π‘œ + 𝑙

slide-62
SLIDE 62

Counting Sort

  • Why not always use counting sort?
  • For 64-bit numbers, requires an array of length 264 > 1019

– 5 GHz CPU will require > 116 years to initialize the array – 18 Exabytes of data

  • Total amount of data that Google has (?)

62

One Exabyte = 1018 bytes 1 million terabytes (TB) 1 billion gigabytes (GB) 100,000 x Library of Congress (print)

slide-63
SLIDE 63

12 Exabytes

63 https://en.wikipedia.org/wiki/Utah_Data_Center

slide-64
SLIDE 64

Radix Sort

  • Idea: Stable sort on each digit, from least

significant to most significant

64

103 801 401 323 255 823 999 101 1 2 3 4 5 6 7

Place each element into a β€œbucket” according to its 1’s place

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

slide-65
SLIDE 65

Radix Sort

  • Idea: Stable sort on each digit, from least

significant to most significant

65

Place each element into a β€œbucket” according to its 10’s place

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

slide-66
SLIDE 66

Radix Sort

  • Idea: Stable sort on each digit, from least

significant to most significant

66

Place each element into a β€œbucket” according to its 100’s place

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

Run Time: 𝑃 𝑒 π‘œ + 𝑐 𝑒 = digits in largest value 𝑐 = base of representation