Week 13 - Friday What did we talk about last time? Exam 2 post - - PowerPoint PPT Presentation

week 13 friday what did we talk about last time exam 2
SMART_READER_LITE
LIVE PREVIEW

Week 13 - Friday What did we talk about last time? Exam 2 post - - PowerPoint PPT Presentation

Week 13 - Friday What did we talk about last time? Exam 2 post mortem Heaps Lab hours Wednesdays at 5 p.m. in The Point 113 Saturdays at noon in The Point 113 (Cancelled this Saturday! Contact Olivia Langley at


slide-1
SLIDE 1

Week 13 - Friday

slide-2
SLIDE 2

 What did we talk about last time?  Exam 2 post mortem  Heaps

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6

 Lab hours

  • Wednesdays at 5 p.m. in The Point 113
  • Saturdays at noon in The Point 113
  • (Cancelled this Saturday! Contact Olivia Langley at
  • livia.langley@otterbein.edu to set up an alternate time)

 CS Club

  • Tuesdays at 5 p.m. in The Point 113 (or next door in The Point 112)
  • (Cancelled next Tuesday due to Thanksgiving!)
slide-7
SLIDE 7
slide-8
SLIDE 8

 It turns out that a heap is a great way to implement a priority

queue

 Although it's useful to think of a heap as a complete binary

tree, almost no one implements them that way

 Instead, we can view the heap as an array  Because it's a complete binary tree, there will be no empty

spots in the array

slide-9
SLIDE 9

 Illustrated:  The left child of element i is at 2i + 1  The right child of element i is at 2i + 2

10 9 3 1

10 9 3 1 1 2 3 4

slide-10
SLIDE 10

public class PriorityQueue { private int[] keys = new int[10]; private int size = 0; … }

slide-11
SLIDE 11

public void insert(int key)

 Always put the key at the end of the array (resizing if

needed)

 The value will often need to be bubbled up, using the

following helper method private void bubbleUp(int index)

slide-12
SLIDE 12

public int max()

 Find the maximum value in the priority queue  Hint: this method is really easy

slide-13
SLIDE 13

public int removeMax()

 Store the value at the top of the heap (array index 0)  Replace it with the last legal value in the array  This value will generally need to be bubbled down, using the

following helper method

  • Bubbling down is harder than bubbling up, because you might have

two legal children!

private void bubbleDown(int index)

slide-14
SLIDE 14
slide-15
SLIDE 15

 Pros:

  • Best, worst, and average case running time of

O(n log n)

  • In-place
  • Good for arrays

 Cons:

  • Not adaptive
  • Not stable
slide-16
SLIDE 16

 Make the array have the heap property:

1.

Let i be the index of the parent of the last two nodes

2.

Bubble the value at index i down if needed

3.

Decrement i

4.

If i is not less than zero, go to Step 2

  • 1. Let pos be the index of the last element in the array
  • 2. Swap index 0 with index pos
  • 3. Bubble down index 0
  • 4. Decrement pos
  • 5. If pos is greater than zero, go to Step 2
slide-17
SLIDE 17

7 45 54 37 108 51 7 45 108 54 37 51 7 54 108 45 37 51 108 54 51 45 37 7

slide-18
SLIDE 18

108 54 51 45 37 7 51 45 7 37 54 108 45 37 7 51 54 108 37 7 45 51 54 108 7 37 45 51 54 108 7 37 45 51 54 108 54 45 51 7 37 108

slide-19
SLIDE 19

 Heap sort is a clever algorithm that uses part of the array to

store the heap and the rest to store the (growing) sorted array

 Even though a priority queue uses both bubble up and bubble

down methods to manage the heap, heap sort only needs bubble down

 You don't need bubble up because nothing is added to the

heap, only removed

slide-20
SLIDE 20
slide-21
SLIDE 21
slide-22
SLIDE 22

 Timsort is a recently developed sorting algorithm used as the

default sort in Python

 It is also used to sort non-primitive arrays in Java  It's a hybrid sort, combining elements of merge sort and insertion

sort

 Features

  • Worst case and average case running time: O(n log n)
  • Best case running time: O(n)
  • Stable
  • Adaptive
  • Not in-place
slide-23
SLIDE 23

 It is similar to when we insertion sorted arrays of

length 10 or smaller

 We also want to find "runs" of data of two kinds:

  • Non-decreasing:

34, 45, 58, 58, 91

  • Strictly decreasing:

85, 67, 24, 18, 7

 These runs are already sorted (or only need a reversal)  If runs are not as long as a minimum run length

determined by the algorithm, the next few values are added in and sorted

 Finally, the sorted runs are merged together  The algorithm can use a specially tuned galloping

mode when merging from two lists

  • Essentially copying in bulk from one list when it knows

that it won't need something from the other for a while

slide-24
SLIDE 24

 It might be useful to implement Timsort in class, but it has a

lot of special cases

 It was developed from both a theoretical perspective but also

with a lot of testing

 If you want to know more, read here:

  • https://www.infopulse.com/blog/timsort-sorting-algorithm/
slide-25
SLIDE 25

 Understanding how sorts work can be challenging  Understanding how running time is affected by various

algorithms and data sets is not obvious

 To help, there are many good visualizations of sorting

algorithms in action:

  • http://www.youtube.com/watch?v=kPRA0W1kECg
  • https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
slide-26
SLIDE 26
slide-27
SLIDE 27

 Tries

slide-28
SLIDE 28

 Keep working on Project 4  Work on Assignment 7

  • Due nextTuesday

 Read Section 5.2