Previous Lecture Lecture 14 Next Lecture

Lecture 14, Thu 05/16

MergeSort and Binary Search Analysis

Plan for today

Class Announcement

Divide and Conquer Algorithms

Mergesort highlights

Mergesort Activity

def sort_sorted_lists(listleft, listright, final):
  # TODO: add solution

The point of this exercise is to create your own solution, so that when you look at the code example in the textbook, it makes more sense how to goes about doing the merging. In this exercise, we are storing the resulting list in the empty list passed through the final parameter, however, in the merge sort, we are operating directly on the larger list, which is why we need the k index – it keeps the position in the list that’s passed directly into the function; we are effectively overwriting its contents, because the values are stored in the left and right copies of the list.

Visualize Mergesort

The visualization of the algorithm

The visualization of O(n log n) derivation

Iclicker check-in

Question 1: Given a list [5, 4, 3, 2, 1], according to the book, what is the 2nd call added to the stack for the merge_sort()?

A. merge_sort([5, 4, 3, 2, 1])
B. merge_sort([5, 4, 3])
C. merge_sort([5, 4])
D. merge_sort([3, 2, 1])
E. merge_sort([2, 1])

Question 2: Given a list [5, 4, 3, 2, 1], according to the book, what is the 3rd call added to the stack for the merge_sort()?

A. merge_sort([5, 4, 3])
B. merge_sort([5, 4])
*C. merge_sort([5])*
D. merge_sort([4])
E. merge_sort([2, 1])

Binary Search