diff options
| author | mo khan <mo@mokhan.ca> | 2021-09-19 16:28:33 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2021-09-19 16:28:33 -0600 |
| commit | d4420cb79a84d0460ddbd1df13ec08c6ca9d77c7 (patch) | |
| tree | ac36f690399a0a94bcf463c08592ded7b4975e8c | |
| parent | cbc47d4e56a04cc4295a0dc5f09b52a356a455f1 (diff) | |
Answer 2.2-3 and 2.2-4
| -rw-r--r-- | notes.md | 25 |
1 files changed, 25 insertions, 0 deletions
@@ -483,3 +483,28 @@ the rate of growth in determining computational efficiency for large inputs. Give the best-case and worst-case running times of selection sort in 𝚯-notation. = 𝚯(n^2). two loops, one nested within the other. + +2.2-3: + +> Consider linear search again (see Exercise 2.1-3). How many elements of the +> input sequence need to be checked on the average, assuming that the element +> being searched for is equally likely to be any element in the array? How about +> in the worst case? What are the average-case and worst-case running times of +> linear search in 𝚯-notation? Justify your answers. + + How many elements of the input sequence need to be checked on the average, assuming that the element being searched for is equally likely to be any element in the array? + + Since there are `n` elements the average would be in the middle (.i.e. n/2). + When we drop the lower order terms this becomes `𝚯(n)`. + + How about in the worst case? + + = 𝚯(n). In the worst case you need to search check every element in the input. + + What are the average-case and worst-case running times of linear search in 𝚯-notation? + + They are both 𝚯(n). + +2.2-4: How can we modify almost any algorithm to have a good best-case running time? + + Reduce the input size by rejecting elements or hard code an early return if some condition is satisfied. |
