Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.

Similar presentations


Presentation on theme: "Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort."— Presentation transcript:

1 Week 13 - Friday

2  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort

3

4

5

6

7

8  Pros:  Best and average case running time of O(n log n)  Very simple implementation  In-place  Ideal for arrays  Cons:  Worst case running time of O(n 2 )  Not stable

9 1. Pick a pivot 2. Partition the array into a left half smaller than the pivot and a right half bigger than the pivot 3. Recursively, quicksort the left half 4. Recursively quicksort the right half

10  Input: array, index, left, right  Set pivot to be array[index]  Swap array[index] with array[right]  Set index to left  For i from left up to right – 1  If array[i] ≤ pivot ▪ Swap array[i] with array[index] ▪ index++  Swap array[index] with array[right]  Return index //so that we know where pivot is

11 7 7 45 0 0 54 37 108 0 0 7 7 45 54 37 108 0 0 7 7 45 54 37 108 0 0 7 7 37 45 54 108 0 0 7 7 37 45 54 108 0 0 7 7 37 45 54 108 0 0 7 7 37 45 54 108

12  Everything comes down to picking the right pivot  If you could get the median every time, it would be great  A common choice is the first element in the range as the pivot  Gives O(n 2 ) performance if the list is sorted (or reverse sorted)  Why?  Another implementation is to pick a random location  Another well-studied approach is to pick three random locations and take the median of those three  An algorithm exists that can find the median in linear time, but its constant is HUGE

13

14  A maximum heap is a complete binary tree where  The left and right children of the root have key values less than the root  The left and right subtrees are also maximum heaps  We can define minimum heaps similarly

15 10 9 9 3 3 0 0 1 1

16  Easy!  We look at the location of the new node in binary  Ignore the first 1, then each 0 is for going left, each 1 is for going right

17  Location: 6  In binary: 110  Right then left 10 9 9 3 3 0 0 1 1

18  Oh no! 10 9 9 3 3 0 0 1 1 15

19 10 9 9 3 3 0 0 1 1 15 10 9 9 15 0 0 1 1 3 3 9 9 10 0 0 1 1 3 3

20 9 9 3 3 0 0 1 1

21 9 9 3 3 0 0 1 1 9 9 3 3 0 0 1 1

22 9 9 3 3 0 0 1 1 1 1 3 3 0 0 9 9

23  Heaps only have:  Add  Remove Largest  Get Largest  Which cost:  Add:O(log n)  Remove Largest:O(log n)  Get Largest:O(1)  Heaps are a perfect data structure for a priority queue

24

25  A priority queue is an ADT that allows us to insert key values and efficiently retrieve the highest priority one  It has these operations:  Insert(key)Put the key into the priority queue  Max()Get the highest value key  Remove Max()Remove the highest value key

26  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

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

28  Illustrated:  The left child of element i is at 2i + 1  The right child of element i is at 2i + 2 10 9 9 3 3 0 0 1 1 9 9 3 3 0 0 1 1 01234

29 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)

30 public int max()  Find the maximum value in the priority queue  Hint: this method is really easy

31 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)

32

33  Pros:  Best, worst, and average case running time of O(n log n)  In-place  Good for arrays  Cons:  Not adaptive  Not stable

34  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

35 7 7 45 0 0 54 37 108 51 7 7 45 108 54 37 0 0 51 7 7 54 108 45 37 0 0 51 108 54 51 45 37 0 0 7 7

36 108 54 51 45 37 0 0 7 7 51 45 0 0 7 7 37 54 108 45 37 0 0 7 7 51 54 108 37 7 7 0 0 45 51 54 108 7 7 0 0 37 45 51 54 108 0 0 7 7 37 45 51 54 108 54 45 51 7 7 37 0 0 108

37  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

38

39

40  Counting sort  Radix sort  Start tries

41  Work on Project 4  Finish Assignment 6  Due today by midnight!  Start on Assignment 7  Due when you return from Thanksgiving  Read sections 5.1 and 5.2  Office hours are canceled today because of a visiting faculty candidate


Download ppt "Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort."

Similar presentations


Ads by Google