Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 12 - Wednesday CS221.

Similar presentations


Presentation on theme: "Week 12 - Wednesday CS221."— Presentation transcript:

1 Week 12 - Wednesday CS221

2 Last time What did we talk about last time? Before that: Exam 2 Review
NP-completeness

3 Questions?

4 Assignment 6

5 Project 4

6 What do we want from sorting?

7 Characteristics of a sort
Running time Best case Worst case Average case Stable Will elements with the same value get reordered? Adaptive Will a mostly sorted list take less time to sort? In-place Can we perform the sort without additional memory? Simplicity of implementation Relates to the constant hidden by Big Oh Online Can sort as numbers arrive

8 Insertion Sort

9 Insertion sort Pros: Cons: Best case running time of O(n) Stable
Adaptive In-place Simple implementation (one of the fastest sorts for 10 elements or fewer!) Online Cons: Worst case running time of O(n2)

10 Insertion sort algorithm
We do n rounds For round i, assume that the elements 0 through i – 1 are sorted Take element i and move it up the list of already sorted elements until you find the spot where it fits

11 Insertion sort example
7 45 54 37 108 51 7 45 54 37 108 51 7 45 54 37 108 51 7 37 45 54 108 51 7 37 45 54 108 51 7 37 45 51 54 108 7 45 54 37 108 51

12 Insertion Sort Implementation

13 Merge Sort

14 Merge sort Pros: Cons: Best, worst, and average case running time of
O(n log n) Stable Ideal for linked lists Cons: Not adaptive Not in-place O(n) additional space needed for an array O(log n) additional space needed for linked lists

15 Merge sort algorithm Take a list of numbers, and divide it in half, then, recursively: Merge sort each half After each half has been sorted, merge them together in order

16 Merge sort example 7 7 45 7 45 7 37 45 54 108 45 45 45 7 45 54 37 108 54 37 108 37 54 108 54 37 108 37 108 37 108

17 Merge Sort Implementation

18 Merge sort methods public static void mergeSort(double[] values) {
double[] scratch = new double[values.length]; mergeSort(values, scratch, 0, values.length); } private static void mergeSort(double[] values, double[] scratch, int start, int end) { private static void merge(double[] values, double[] scratch, int start, int mid, int end) {

19 Quicksort

20 Quicksort Pros: Cons: 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(n2) Not stable

21 Quicksort algorithm Pick a pivot
Partition the array into a left half smaller than the pivot and a right half bigger than the pivot Recursively, quicksort the left half Recursively quicksort the right half

22 Partition algorithm 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++ Return index //so that we know where pivot is

23 Quicksort Example 7 45 54 37 108 7 45 54 37 108 7 45 54 37 108 7 37 45 54 108 7 37 45 54 108 7 37 45 54 108 7 37 45 54 108

24 Quicksort issues 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(n2) 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

25 Quiz

26 Upcoming

27 Next time… Quicksort Heaps Heapsort TimSort Counting sort

28 Reminders Work on Project 4 Read section 2.4


Download ppt "Week 12 - Wednesday CS221."

Similar presentations


Ads by Google