CMU 15-112: Fundamentals of Programming and Computer Science
Collab 10 (Due Friday 6-Nov in your assigned recitation)
- Discuss Linear Search and Binary Search
Discuss linear search and binary search from here. - Review Efficiency lecture notes
Review the notes from the Efficiency lecture. In particular, be sure you understand:- What (approximately) 210, 220, and 230 equal.
- What approximately log2(1 thousand), log2(1 million), and log2(1 billion) equal.
- The fact that logn is much, much smaller than n.
- Why we ignore lower-order terms and constants in Big-O.
- Why we ignore the base in logs in Big-O.
- How selectionSort and mergeSort work.
- The proof that selectionSort is O(n2).
- The proof that, for selectionSort (or any quadratic algorithm), when you multiply the input size by c, you multiply the runtime by c2.
- The proof that mergeSort is O(nlogn).
- The fact that, for mergeSort (or any nlogn algorithm), when you multiply the input size by c, you multiply the runtime by a value slightly larger than c but quite a bit smaller than c2.
- The fact that O(nlogn) is theoretically optimal for any comparison-based sort, and consequently that mergeSort is within a constant time as fast as any possible comparison-based sort.
- Review xSortLab
Run xSortLab, and...- For each of selectionSort and mergeSort:
- Do a visual sort and predict each step before confirming it visually. Do this for several steps. In particular, if shown a picture of a given sort at any stage, be sure to be able to answer which two values are the next to be compared, and which values are the next to be swapped (for selectionSort) or copied (for mergeSort).
- Be able to explain what constitutes one "pass" for each of these sorts.
- Do a timed sort of selectionSort, and confirm that doubling the input size increases the runtime by about 4x, and that this prediction grows more accurate as the input size increases.
- Do a timed sort of mergeSort, and confirm that doubling the input size increases the runtime by more than 2x, but just barely, and well under 4x, and that this prediction grows more accurate as the input size increases.
- For each of selectionSort and mergeSort:
- Short Answers
- For each of these functions, note the closest family it belongs to
(big O notation).
A. T = 2N + 5 O(logN) O(N) O(NlogN) O(N**2) O(N**3) B. T = N + logN O(logN) O(N) O(NlogN) O(N**2) O(N**3) C. T = N**3 + 45N**2 + 100logN O(logN) O(N) O(NlogN) O(N**2) O(N**3) D. T = 10N * 3N O(logN) O(N) O(NlogN) O(N**2) O(N**3) E. T = 10N + 3N O(logN) O(N) O(NlogN) O(N**2) O(N**3) F. T = number of steps merge sort takes to sort a list of N numbers O(logN) O(N) O(NlogN) O(N**2) O(N**3)
- Briefly describe how a hashtable works for a set, and in particular explain why checking if a value is in a set is O(1).
- Answer the following questions.
Wherever it is relevant, assume that all the functions run in one of O(1), O(logN), O(N), O(NlogN), O(N**2), O(N**3), or O(2**N).- Your code takes 5 seconds to run on an input of length 1000. How long does your code take to run on an input of length 3000 if it runs in O(n**2)?
- Your code takes 5 seconds to run on an input of length 1000 and 10 seconds to run on an input of length 2000. What is the most likely big oh?
- Your code takes 5 seconds to run on an input of length 1000 and 11 seconds to run on an input of length 2000. What is the most likely big oh?
- Your code takes 5 seconds to run on an input of length 1000 and 40 seconds to run on an input of length 2000. What is the most likely big oh?
- Which grows faster, log(N) or N**0.5 (square root of N)?
- SubsetSum(L) is a famous problem where we try to find some values in L such that they sum to zero. The naive solution to subsetSum runs in O(2**N) time. That is, exponential time. This is awful. To see how awful, if your code takes 1 second to solve subsetSum on an input of size 10, how many years would it take to solve subsetSum on an input of size 50 (yes, just 50)?
- For each of these functions, note the closest family it belongs to
(big O notation).
- Rewrite (from scratch, without notes) Selection and Merge Sort
Note: in recitation, you will all just discuss the implementations of selectionSort and mergeSort from the notes here. Sometime soon after recitation, though, students are all expected to finish this task, and to rewrite the code from scratch for both of these sorts. You are responsible for all the code in that file, including selectionSort, mergeSort (and merge), and the timing and testing code. Be sure to study our implementations, and not others online (and be sure not to use recursion!). You need to know this code very well by the upcoming quiz!