Divide-and-Conquer made easier by dividing it up across ...

127
Divide-and-Conquer Sorting Algorithms What is an example of a real-world problem that's made easier by dividing it up across many people? (put your answers the chat)

Transcript of Divide-and-Conquer made easier by dividing it up across ...

Page 1: Divide-and-Conquer made easier by dividing it up across ...

Divide-and-Conquer Sorting Algorithms

What is an example of a real-world problem that's made easier by dividing it up across many people?

(put your answers the chat)

Page 2: Divide-and-Conquer made easier by dividing it up across ...

vectors + grids

stacks + queues

sets + maps

Object-Oriented Programming

arrays

dynamic memory management

linked data structures

algorithmic analysistesting

recursive problem-solving

Roadmap

Life after CS106B!

C++ basics

Diagnostic

real-world algorithms

Core Tools

User/clientImplementation

Page 3: Divide-and-Conquer made easier by dividing it up across ...

vectors + grids

stacks + queues

sets + maps

Object-Oriented Programming

algorithmic analysistesting

recursive problem-solving

Roadmap

Life after CS106B!

C++ basics

Diagnostic

real-world algorithms

Core Tools

User/client arrays

dynamic memory management

linked data structures

Implementation

real-world algorithms

Page 4: Divide-and-Conquer made easier by dividing it up across ...

Today’s questions

How can we design better, more efficient sorting algorithms?

Page 5: Divide-and-Conquer made easier by dividing it up across ...

Today’s topics

1. Review

2. Merge Sort

3. Quicksort

Page 6: Divide-and-Conquer made easier by dividing it up across ...

Review[linked list wrapup + intro to sorting]

Page 7: Divide-and-Conquer made easier by dividing it up across ...

Linked List Wrapup

Page 8: Divide-and-Conquer made easier by dividing it up across ...

Common linked lists operations

● Traversal○ How do we walk through all elements in the linked list?

● Rewiring○ How do we rearrange the elements in a linked list?

● Insertion○ How do we add an element to a linked list?

● Deletion○ How do we remove an element from a linked list?

Page 9: Divide-and-Conquer made easier by dividing it up across ...

Takeaways for manipulating the middle of a list

● While traversing to where you want to add/remove a node, you’ll often want to keep track of both a current pointer and a previous pointer.○ This makes rewiring easier between the two!○ This also means you have to check that neither is nullptr before dereferencing.

0x26b0

head

Nod

e* "Kylie""Nick" PTR"Trip"

0xbc70

prev

Nod

e*

0x40f0

cur

Nod

e*

Page 10: Divide-and-Conquer made easier by dividing it up across ...

Linked list summary

● You’ve now learned lots of ways to manipulate linked lists!○ Traversal○ Rewiring○ Insertion (front/back/middle)○ Deletion (front/back/middle)

● You’ve seen linked lists in classes and outside classes, and pointers passed by value and passed by reference.

● Assignment 5 will really test your understanding of linked lists.○ Draw lots of pictures!○ Test small parts of your code at a time to make sure individual operations are working correctly.

Page 11: Divide-and-Conquer made easier by dividing it up across ...

Sorting

Page 12: Divide-and-Conquer made easier by dividing it up across ...

sortingGiven a list of data points, sort those

data points into ascending / descending order by some quantity.

Definition

Page 13: Divide-and-Conquer made easier by dividing it up across ...

Selection sort takeaways

● Selection sort works by "selecting" the smallest remaining element in the list and putting it in the front of all remaining elements.

● Selection sort is an O(n2) algorithm.

Page 14: Divide-and-Conquer made easier by dividing it up across ...

Insertion sort algorithm

● Repeatedly insert an element into a sorted sequence at the front of the array.

● To insert an element, swap it backwards until either:○ (1) it’s bigger than the element before it, or○ (2) it’s at the front of the array.

Page 15: Divide-and-Conquer made easier by dividing it up across ...

The complexity of insertion sort

● In the worst case (the array is in reverse sorted order), insertion sort takes time O(n2).

○ The analysis for this is similar to selection sort!

● In the best case (the array is already sorted), insertion takes time O(n) because you only iterate through once to check each element.

○ Selection sort, however, is always O(n2) because you always have to search the remainder of the list to guarantee that you’re finding the minimum at each step.

● Fun fact: Insertion sorting an array of random values takes, on average, O(n2) time.

○ This is beyond the scope of the class – take CS109 if you’re interested in learning more!

Page 16: Divide-and-Conquer made easier by dividing it up across ...

Let's do better than O(N2) sorting!

Page 17: Divide-and-Conquer made easier by dividing it up across ...

Advanced Sorting

Page 18: Divide-and-Conquer made easier by dividing it up across ...

How can we design better, more efficient sorting

algorithms?

Page 19: Divide-and-Conquer made easier by dividing it up across ...

Divide-and-Conquer

Page 20: Divide-and-Conquer made easier by dividing it up across ...

Motivating Divide-and-Conquer

● So far, we've seen O(N2) sorting algorithms. How can we start to do better?

Page 21: Divide-and-Conquer made easier by dividing it up across ...

Motivating Divide-and-Conquer

● So far, we've seen O(N2) sorting algorithms. How can we start to do better?

● Assume that it takes t seconds to run insertion sort on the following array:

Page 22: Divide-and-Conquer made easier by dividing it up across ...

Motivating Divide-and-Conquer

● So far, we've seen O(N2) sorting algorithms. How can we start to do better?

● Assume that it takes t seconds to run insertion sort on the following array:

● Poll: Approximately how many seconds will it take to run insertion sort on each of the following arrays?

Page 23: Divide-and-Conquer made easier by dividing it up across ...

Motivating Divide-and-Conquer

● So far, we've seen O(N2) sorting algorithms. How can we start to do better?

● Assume that it takes t seconds to run insertion sort on the following array:

● Poll: Approximately how many seconds will it take to run insertion sort on each of the following arrays?

Answer: Each arr y sho ld o ly take bo t t/4 seconds o sor .

Page 24: Divide-and-Conquer made easier by dividing it up across ...

Motivating Divide-and-Conquer

● Main insight:○ Sorting N elements directly takes total time t○ Sorting two sets of N/2 elements (total of N elements) takes total time t/2○ We got a speedup just by sorting smaller sets of elements at a time!

Page 25: Divide-and-Conquer made easier by dividing it up across ...

Motivating Divide-and-Conquer

● Main insight:○ Sorting N elements directly takes total time t○ Sorting two sets of N/2 elements (total of N elements) takes total time t/2○ We got a speedup just by sorting smaller sets of elements at a time!

● The main idea behind divide-and-conquer algorithms takes advantage of this. Let's design algorithms that break up a problem into many smaller problems that can be solved in parallel!

Page 26: Divide-and-Conquer made easier by dividing it up across ...

General Divide-and-Conquer Approach

● Our general approach when designing a divide-and-conquer algorithm is to decide how to make the problem smaller and how to unify the results of these solved, smaller problems.

Page 27: Divide-and-Conquer made easier by dividing it up across ...

General Divide-and-Conquer Approach

● Our general approach when designing a divide-and-conquer algorithm is to decide how to make the problem smaller and how to unify the results of these solved, smaller problems.

● Both sorting algorithms we explore today will have both of these components:○ Divide Step

■ Make the problem smaller by splitting up the input list○ Join Step

■ Unify the newly sorted sublists to build up the overall sorted result

Page 28: Divide-and-Conquer made easier by dividing it up across ...

General Divide-and-Conquer Approach

● Our general approach when designing a divide-and-conquer algorithm is to decide how to make the problem smaller and how to unify the results of these solved, smaller problems.

● Both sorting algorithms we explore today will have both of these components:○ Divide Step

■ Make the problem smaller by splitting up the input list○ Join Step

■ Unify the newly sorted sublists to build up the overall sorted result

● Divide-and-Conquer is a ripe time to return to recursion!

Page 29: Divide-and-Conquer made easier by dividing it up across ...

Merge Sort

Page 30: Divide-and-Conquer made easier by dividing it up across ...

Merge Sort

A recursive sorting algorithm!

● Base Case:○ An empty or single-element list is already sorted.

● Recursive step:○ Break the list in half and recursively sort each part. (easy divide)○ Use merge to combine them back into a single sorted list (hard join)

Page 31: Divide-and-Conquer made easier by dividing it up across ...
Page 32: Divide-and-Conquer made easier by dividing it up across ...
Page 33: Divide-and-Conquer made easier by dividing it up across ...
Page 34: Divide-and-Conquer made easier by dividing it up across ...
Page 35: Divide-and-Conquer made easier by dividing it up across ...
Page 36: Divide-and-Conquer made easier by dividing it up across ...

What do we do now? Whe does the sor ing magi h pen?

Page 37: Divide-and-Conquer made easier by dividing it up across ...

The Key Insight: Merge

Page 38: Divide-and-Conquer made easier by dividing it up across ...

The Key Insight: Merge

Page 39: Divide-and-Conquer made easier by dividing it up across ...

The Key Insight: Merge

Page 40: Divide-and-Conquer made easier by dividing it up across ...

The Key Insight: Merge

Page 41: Divide-and-Conquer made easier by dividing it up across ...

The Key Insight: Merge

Page 42: Divide-and-Conquer made easier by dividing it up across ...

The Key Insight: Merge

Page 43: Divide-and-Conquer made easier by dividing it up across ...

The Key Insight: Merge

Page 44: Divide-and-Conquer made easier by dividing it up across ...

The Key Insight: Merge

Page 45: Divide-and-Conquer made easier by dividing it up across ...

The Key Insight: Merge

Page 46: Divide-and-Conquer made easier by dividing it up across ...

The Key Insight: Merge

Page 47: Divide-and-Conquer made easier by dividing it up across ...

The Key Insight: Merge

Page 48: Divide-and-Conquer made easier by dividing it up across ...

The Key Insight: Merge

Page 49: Divide-and-Conquer made easier by dividing it up across ...

The Key Insight: Merge

Page 50: Divide-and-Conquer made easier by dividing it up across ...

The Key Insight: Merge

Each te m kes a ingle comparison and reduce the number o eleme s one. If there re ot l eleme s, thi algorithm ru in time O(n).

Page 51: Divide-and-Conquer made easier by dividing it up across ...

The Key Insight: Merge

● The merge algorithm takes in two sorted lists and combines them into a single sorted list.

● While both lists are nonempty, compare their first elements. Remove the smaller element and append it to the output.

● Once one list is empty, add all elements from the other list to the output.

Page 52: Divide-and-Conquer made easier by dividing it up across ...
Page 53: Divide-and-Conquer made easier by dividing it up across ...
Page 54: Divide-and-Conquer made easier by dividing it up across ...
Page 55: Divide-and-Conquer made easier by dividing it up across ...
Page 56: Divide-and-Conquer made easier by dividing it up across ...
Page 57: Divide-and-Conquer made easier by dividing it up across ...
Page 58: Divide-and-Conquer made easier by dividing it up across ...
Page 59: Divide-and-Conquer made easier by dividing it up across ...

Merge Sort

A recursive sorting algorithm!

● Base Case:○ An empty or single-element list is already sorted.

● Recursive step:○ Break the list in half and recursively sort each part. (easy divide)○ Use merge to combine them back into a single sorted list (hard join)

Page 60: Divide-and-Conquer made easier by dividing it up across ...

Merge Sort – Let's code it!

Page 61: Divide-and-Conquer made easier by dividing it up across ...

Analyzing Mergesort:

How fast is this sorting algorithm?

Page 62: Divide-and-Conquer made easier by dividing it up across ...

void mergeSort(Vector<int>& vec) {

/* A list with 0 or 1 elements is already sorted by definition. */

if (vec.size() <= 1) return;

/* Split the list into two, equally sized halves */

Vector<int> left, right;

split(vec, left, right);

/* Recursively sort the two halves. */

mergeSort(left);

mergeSort(right);

/*

* Empty out the original vector and re-fill it with merged result

* of the two sorted halves.

*/

vec = {};

merge(vec, left, right);

}

Page 63: Divide-and-Conquer made easier by dividing it up across ...

void mergeSort(Vector<int>& vec) {

/* A list with 0 or 1 elements is already sorted by definition. */

if (vec.size() <= 1) return;

/* Split the list into two, equally sized halves */

Vector<int> left, right;

split(vec, left, right);

/* Recursively sort the two halves. */

mergeSort(left);

mergeSort(right);

/*

* Empty out the original vector and re-fill it with merged result

* of the two sorted halves.

*/

vec = {};

merge(vec, left, right);

}

O(n) work

O(n) work

Page 64: Divide-and-Conquer made easier by dividing it up across ...

void mergeSort(Vector<int>& vec) {

/* A list with 0 or 1 elements is already sorted by definition. */

if (vec.size() <= 1) return;

/* Split the list into two, equally sized halves */

Vector<int> left, right;

split(vec, left, right);

/* Recursively sort the two halves. */

mergeSort(left);

mergeSort(right);

/*

* Empty out the original vector and re-fill it with merged result

* of the two sorted halves.

*/

vec = {};

merge(vec, left, right);

}

Page 65: Divide-and-Conquer made easier by dividing it up across ...

O(n)

O(n)

O(n)

O(n)

O(n)

O(n) work at each level!

Page 66: Divide-and-Conquer made easier by dividing it up across ...

O(n)

O(n)

O(n)

O(n)

O(n)

How many levels are there?

Page 67: Divide-and-Conquer made easier by dividing it up across ...

O(n)

O(n)

O(n)

O(n)

O(n)

Remember: How many times do we divide by 2?

Page 68: Divide-and-Conquer made easier by dividing it up across ...

O(n)

O(n)

O(n)

O(n)

O(n)

O(log n) levels!

Page 69: Divide-and-Conquer made easier by dividing it up across ...

O(n)

O(n)

O(n)

O(n)

O(n)

Total work: O(n * log n)

Page 70: Divide-and-Conquer made easier by dividing it up across ...

void mergeSort(Vector<int>& vec) {

/* A list with 0 or 1 elements is already sorted by definition. */

if (vec.size() <= 1) return;

/* Split the list into two, equally sized halves */

Vector<int> left, right;

split(vec, left, right);

/* Recursively sort the two halves. */

mergeSort(left);

mergeSort(right);

/*

* Empty out the original vector and re-fill it with merged result

* of the two sorted halves.

*/

vec = {};

merge(vec, left, right);

}

O(n log n) work

Page 71: Divide-and-Conquer made easier by dividing it up across ...

Analyzing Mergesort: Can we do better?

● Mergesort runs in time O(n log n), which is faster than insertion sort’s O(n2).○ Can we do better than this?

● A comparison sort is a sorting algorithm that only learns the relative ordering of its elements by making comparisons between elements.

○ All of the sorting algorithms we’ve seen so far are comparison sorts.

● Theorem: There are no comparison sorts whose average-case runtime can be better than O(n log n).

● If we stick with making comparisons, we can only hope to improve on mergesort by a constant factor!

Page 72: Divide-and-Conquer made easier by dividing it up across ...

A Quick Historical Aside

● Mergesort was one of the first algorithms developed for computers as we know them today.

● It was invented by John von Neumann in 1945 (!) as a way of validating the design of the first “modern” (stored-program) computer.

● Want to learn more about what he did? Check out this article by Stanford’s very own Donald Knuth.

Page 73: Divide-and-Conquer made easier by dividing it up across ...

Announcements

Page 74: Divide-and-Conquer made easier by dividing it up across ...

Announcements

● Assignment 5 was released yesterday and will be due on Tuesday, August 4 at 11:59pm PDT.

● The Assignment 5 YEAH session will take place tonight at 6pm PDT.

● Diagnostic regrades are due on Thursday, July 30 at 11:59 PDT.○ All grades have now been finalized in Gradescope.

● Come talk to us (Nick/Kylie/Trip/an SL) about your final projects this week!○ We’re here to help you scope the problems and find something that interests you.

Page 75: Divide-and-Conquer made easier by dividing it up across ...

Lecture Tomorrow

● We really want to make sure folks are staying on track to work on the final project, so we are replacing tomorrow's lecture with an open-invite final project advising session.

● We will still meet in our normal lecture Zoom room from 11:30am-12:30pm PDT.

● Attendance is optional, and we will not be recording the session.

● If you haven't yet talked to us about your idea, we recommend coming by and chatting with us. If you're well on track on the final project, then enjoy the day off!

Page 76: Divide-and-Conquer made easier by dividing it up across ...

Quicksort

Page 77: Divide-and-Conquer made easier by dividing it up across ...

Quicksort Algorithm

1. Partition the elements into three categories based on a chosen pivot element:○ Elements smaller than the pivot○ Elements equal to the pivot○ Elements larger than the pivot

Page 78: Divide-and-Conquer made easier by dividing it up across ...

Quicksort Algorithm

1. Partition the elements into three categories based on a chosen pivot element:○ Elements smaller than the pivot○ Elements equal to the pivot○ Elements larger than the pivot

Our divide step (hard divide)!

Page 79: Divide-and-Conquer made easier by dividing it up across ...

Quicksort Algorithm

1. Partition the elements into three categories based on a chosen pivot element:○ Elements smaller than the pivot○ Elements equal to the pivot○ Elements larger than the pivot

2. Recursively sort the two partitions that are not equal to the pivot (smaller and larger elements).○ Now our smaller elements are in sorted order, and our larger elements are also in

sorted order!

Page 80: Divide-and-Conquer made easier by dividing it up across ...

Quicksort Algorithm

1. Partition the elements into three categories based on a chosen pivot element:○ Elements smaller than the pivot○ Elements equal to the pivot○ Elements larger than the pivot

2. Recursively sort the two partitions that are not equal to the pivot (smaller and larger elements).○ Now our smaller elements are in sorted order, and our larger elements are also in

sorted order!

3. Concatenate the three now-sorted partitions together.

Page 81: Divide-and-Conquer made easier by dividing it up across ...

Quicksort Algorithm

1. Partition the elements into three categories based on a chosen pivot element:○ Elements smaller than the pivot○ Elements equal to the pivot○ Elements larger than the pivot

2. Recursively sort the two partitions that are not equal to the pivot (smaller and larger elements).○ Now our smaller elements are in sorted order, and our larger elements are also in

sorted order!

3. Concatenate the three now-sorted partitions together. Our join step! (easy join)

Page 82: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

Page 83: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

Choose he fir t element a the pivot.

Page 84: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

Parti ion element into maller th n, eq l o, and re ter than the pivot.

Page 85: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

Page 86: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

Rec r ivel sor the maller rtition for piv 14!

Page 87: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

Choose he fir t element a the pivot.

Page 88: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

Parti ion element into maller th n, eq l o, and re ter than the pivot.

Page 89: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311

Page 90: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311

Rec r ivel sor the maller par itio or piv 12!

Page 91: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311

Onl one element o we’re done!

Page 92: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311

Rec r ivel sor the larger par itio or piv 12!

Page 93: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311

Onl one element o we’re done!

Page 94: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311Now e n concatenate sm ller than, eq l o, and re ter than

for he piv 12.

Page 95: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311

11 1312

Page 96: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311

11 1312

Rec r ivel sor the larger par itio or piv 14!

Page 97: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311

11 1312

Choose he fir t element a the pivo .

Page 98: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311

11 1312

Parti ion element into maller than, eq l o, and re ter than

the pivot.

Page 99: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311

11 1312

15

Rec r ivel sor the maller par itio or piv 16!

Page 100: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311

11 1312

15

Onl one element o we’re done!

Page 101: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311

11 1312

15

Rec r ivel sor the larger par itio or piv 16!

Page 102: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311

11 1312

15

No elements in th par itio o we’re done!

Page 103: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311

11 1312

15

Now e n concatenate sm ller than, eq l o, and re ter than

for he piv 16.

Page 104: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311

11 1312

15

15 16

Page 105: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311

11 1312

15

15 16

Now e n concatenate sm ller than, eq l o, and re ter than

for he piv 14. (the origi al pivo !)

Page 106: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311

11 1312

15

15 1614

Page 107: Divide-and-Conquer made easier by dividing it up across ...

14 12 16 13 11 15Input of unsorted elements:

12 1613 11 15

1311

11 1312

15

15 1614 Sorted!

Page 108: Divide-and-Conquer made easier by dividing it up across ...

Quicksort Algorithm

1. Partition the elements into three categories based on a chosen pivot element:○ Elements smaller than the pivot○ Elements equal to the pivot○ Elements larger than the pivot

2. Recursively sort the two partitions that are not equal to the pivot (smaller and larger elements).○ Now our smaller elements are in sorted order, and our larger elements are also in

sorted order!

3. Concatenate the three now-sorted partitions together.

Page 109: Divide-and-Conquer made easier by dividing it up across ...

Quicksort – Let's code it!

Page 110: Divide-and-Conquer made easier by dividing it up across ...

Quicksort Takeaways

● Our “divide” step = partitioning elements based on a pivot

● Our recursive call comes in between dividing and joining○ Base case: One element or no elements to sort!

● Our “join” step = combining the sorted partitions

● Unlike in merge sort where most of the sorting work happens in the “join” step, our sorting work occurs primarily at the “divide” step for quicksort (when we sort elements into partitions).

Page 111: Divide-and-Conquer made easier by dividing it up across ...

Quicksort Efficiency Analysis

● Similar to Merge Sort, Quicksort also has O(N log N) runtime in the average case.

○ With good choice of pivot, we split the initial list into roughly two equally-sized parts every time. ○ Thus, we reach a depth of about log N split operations before reaching the base case.○ At each level, we do O(n) work to partition and concatenate.

Page 112: Divide-and-Conquer made easier by dividing it up across ...

Quicksort Efficiency Analysis

● Similar to Merge Sort, Quicksort also has O(N log N) runtime in the average case.

○ With good choice of pivot, we split the initial list into roughly two equally-sized parts every time. ○ Thus, we reach a depth of about log N split operations before reaching the base case.○ At each level, we do O(n) work to partition and concatenate.

● However, Quicksort performance can degrade to O(N2) with poor choice of pivot!

○ Come talk to us after class if you're interested in why!

Page 113: Divide-and-Conquer made easier by dividing it up across ...

Quicksort Efficiency Analysis

● Similar to Merge Sort, Quicksort also has O(N log N) runtime in the average case.

○ With good choice of pivot, we split the initial list into roughly two equally-sized parts every time. ○ Thus, we reach a depth of about log N split operations before reaching the base case.○ At each level, we do O(n) work to partition and concatenate.

● However, Quicksort performance can degrade to O(N2) with poor choice of pivot!

○ Come talk to us after class if you're interested in why!

● The ultimate question: Can we do better?○ From a space efficiency perspective, yes, there are versions of Quicksort that don't require

making many copies of the list (in-place Quicksort). But from a runtime efficiency perspective...

Page 114: Divide-and-Conquer made easier by dividing it up across ...

The Limit Does Exist

● There is a fundamental limit on the efficiency of comparison-based sorting algorithms.

Page 115: Divide-and-Conquer made easier by dividing it up across ...

The Limit Does Exist

● There is a fundamental limit on the efficiency of comparison-based sorting algorithms.

● You can prove that it is not possible to guarantee a list has been sorted unless you have done at minimum O(N log N) comparisons.

○ Take CS161 to learn how to write this proof!

Page 116: Divide-and-Conquer made easier by dividing it up across ...

The Limit Does Exist

● There is a fundamental limit on the efficiency of comparison-based sorting algorithms.

● You can prove that it is not possible to guarantee a list has been sorted unless you have done at minimum O(N log N) comparisons.

○ Take CS161 to learn how to write this proof!

● Thus, we can't do better (in Big-O terms at least) than Merge Sort and Quicksort!

○ Take CS161 to learn about how there are actually clever non-comparison-based sorting algorithms that are able to break this limit.

Page 117: Divide-and-Conquer made easier by dividing it up across ...

Final Advice

Page 118: Divide-and-Conquer made easier by dividing it up across ...

Assignment 5 Tips

● When implementing the two sorting algorithms, it is strongly recommended to implement helper functions for the divide/join components of the algorithm.

○ For merge sort this means having helper functions for the split and merge operations○ For quicksort this means having helper functions for the partition and concatenate operations

Page 119: Divide-and-Conquer made easier by dividing it up across ...

Assignment 5 Tips

● When implementing the two sorting algorithms, it is strongly recommended to implement helper functions for the divide/join components of the algorithm.

○ For merge sort this means having helper functions for the split and merge operations○ For quicksort this means having helper functions for the partition and concatenate operations

● These helper functions should be implemented iteratively, but the overall sorting algorithms themselves operate recursively. Mind the distinction!

Page 120: Divide-and-Conquer made easier by dividing it up across ...

Assignment 5 Tips

● When implementing the two sorting algorithms, it is strongly recommended to implement helper functions for the divide/join components of the algorithm.

○ For merge sort this means having helper functions for the split and merge operations○ For quicksort this means having helper functions for the partition and concatenate operations

● These helper functions should be implemented iteratively, but the overall sorting algorithms themselves operate recursively. Mind the distinction!

● Write tests for your helper functions first! Then, write end-to-end tests for your sorting functions.

Page 121: Divide-and-Conquer made easier by dividing it up across ...

Summary

Page 122: Divide-and-Conquer made easier by dividing it up across ...

https://www.toptal.com/developers/sorting-algorithms

Page 123: Divide-and-Conquer made easier by dividing it up across ...
Page 124: Divide-and-Conquer made easier by dividing it up across ...

Sorting

● Sorting is a powerful tool for organizing data in a meaningful format!

● There are many different methods for sorting data:○ Selection Sort○ Insertion Sort○ Mergesort○ Quicksort○ And many more…

● Understanding the different runtimes and tradeoffs of the different algorithms is important when choosing the right tool for the job!

Page 125: Divide-and-Conquer made easier by dividing it up across ...

What’s next?

Page 126: Divide-and-Conquer made easier by dividing it up across ...

vectors + grids

stacks + queues

sets + maps

Object-Oriented Programming

algorithmic analysistesting

recursive problem-solving

Roadmap

Life after CS106B!

C++ basics

Diagnostic

real-world algorithms

Core Tools

User/client arrays

dynamic memory management

linked data structures

Implementation

Page 127: Divide-and-Conquer made easier by dividing it up across ...

Trees!