From 369965d5b6031ef5b2bf3e69ef2a88f57919cd04 Mon Sep 17 00:00:00 2001 From: "Brady J. Garvin" <bgarvin@cse.unl.edu> Date: Tue, 25 Oct 2022 16:46:20 -0500 Subject: [PATCH] Designed and traced quickmedian. --- divide-and-conquer/notes.md | 55 +++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/divide-and-conquer/notes.md b/divide-and-conquer/notes.md index f4a53c4..df4fe2a 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) -- GitLab