CS140-lecture-20210927

Divide and conquer #

image_2021-09-27-16-14-55 image_2021-09-27-16-16-00

\[\begin{aligned} T(n) &= 2T \left( \frac{n}{2} \right) + \Theta (n) \\ &= \Theta(n\log n) &\text{divide and conquer}\\ T(n) &= T(n-1) + \Theta(n) \\ &= \Theta(n^2) &\text{naive approach} \end{aligned}\]

A problem divided into any ratio, with the rest of the problem a complement of the original input, the overall complexity will still be \( \Theta(n \lg n) \) .

image_2021-09-27-16-19-18

Mergesort #

image_2021-09-27-16-19-44 image_2021-09-27-16-20-13 image_2021-09-27-16-22-22 image_2021-09-27-16-23-43 image_2021-09-27-16-23-53 image_2021-09-27-16-25-35 image_2021-09-27-16-25-39 image_2021-09-27-16-26-05

Mergesort does not sort in place. During merge, it copies each subarray to a new array, then places them back into the original.

image_2021-09-27-16-28-21 image_2021-09-27-16-29-23 image_2021-09-27-16-30-09 image_2021-09-27-16-30-11 image_2021-09-27-16-30-13

Merge function #

image_2021-09-27-16-30-26 image_2021-09-29-09-44-56

Analyzing divide and conquer algorithms #

image_2021-09-29-09-46-24 image_2021-09-29-10-01-22

Advantages and disadvantages #

image_2021-09-29-10-01-24

Sorting challenges #

The sorting problem is generally sorting the keys of records.

image_2021-09-29-10-02-30

While some algorithms time complexity, like selection/bubble/insertion, may have a runtime complexity of \( O(n^2) \) , the actual comparisons and swapping during the algorithm may happen more than that amount. However, since we are just comparing the keys, and not the huge records, we don’t have to take this into account.

image_2021-09-29-10-09-23

This examples shows us that merely comparing the runtime complexity is not always the best way of comparing algorithms. For example, if we are swapping huge records, then the algorithm with the least amount of swaps will be best (even if it has a worse runtime complexity).

image_2021-09-29-10-11-56 image_2021-09-29-10-12-00

In this problem, since the number of elements is huge, we don’t want to choose the algorithm that has the last amount of swaps. The actual amount of comparisons will be a better way to choose our algorithm. In this case, we will choose mergesort.

image_2021-09-29-10-13-48 image_2021-09-29-10-13-54

If we have an array that is almost sorted, then insertion sort will do the best. Bubble sort will have to still do \( n^2 \) comparisons, even if only 1 element is out of order (the lowest at the end).

image_2021-09-29-10-17-33