diff --git a/divide-and-conquer/notes.md b/divide-and-conquer/notes.md index f4a53c4c55246136c143f53a7ade40e8cbd3b2ee..df4fe2a7b571f80b551fc80b2259aaf8094e4acf 100644 --- a/divide-and-conquer/notes.md +++ b/divide-and-conquer/notes.md @@ -69,11 +69,11 @@ Problem: Find the median of an odd-length list in average-case Θ(n) time. Use ``` In: [8, 7, 6, 9, 5] - Out: … + Out: ???? / \ / \ - In: […] In: […] - Out: … Out: … + In: [8, 7] In: [6, 9, 5] + Out: 7.5 Out: 6 ``` * Option A: We are missing information from the recursive calls. Add output(s) so that they can return that information. @@ -81,15 +81,15 @@ Problem: Find the median of an odd-length list in average-case Θ(n) time. Use ## Design (second attempt) -* More generally, we want …, so we add an input `k`: +* More generally, we want kth smallest element, so we add an input `k`: ``` In: [8, 7, 6, 9, 5], k = 2 Out: … / \ / \ - In: […], k = 2 In: […], k = 0 - Out: … Out: … + In: [8, 7], k = 2 In: [6, 9, 5], k = 0 + Out: ???? Out: 5 ``` ## Partitioning @@ -104,24 +104,39 @@ Problem: Find the median of an odd-length list in average-case Θ(n) time. Use ``` In: [8, 7, 6, 9, 5], k = 2 - In: […], k = 2 - In: […], k = 2 - Split: […], …, […] - Out: … + * + i + j + In: [8, 7, 6, 5, 9], k = 2 + * + i (crossed) + j + i + In: [5, 7, 6, 8, 9], k = 2 + Split: [5, 7, 6], 8, [9] + Out: 7 | | | - In: […], k = … - In: […], k = … - Split: […], …, […] - Out: … + In: [5, 7, 6], k = 2 + * + i (crossed) + j + i + In: [5, 7, 6], k = 2 + Split: [], 5, [7, 6] + Out: 7 | | | - In: […], k = … - In: […], k = … - Split: […], …, […] - Out: … + In: [7, 6], k = 1 + * + i (crossed) + j + i + In: [6, 7], k = 1 + Split: [6], 7, [] + Out: 7 ``` ## Analysis @@ -129,9 +144,9 @@ Problem: Find the median of an odd-length list in average-case Θ(n) time. Use * Uneven split: T(n) = T(n - 1) + Θ(n) for n ≫ 0 - T(n) = Θ(⋯) + T(n) = Θ(n²) * Even split: T(n) ≈ T(n/2) + Θ(n) for n ≫ 0 - T(n) = Θ(⋯) + T(n) = Θ(n)