From a5cd69394d19afb854a537283fa447570da12007 Mon Sep 17 00:00:00 2001
From: "Brady J. Garvin" <bgarvin@cse.unl.edu>
Date: Wed, 11 Oct 2023 14:05:51 -0500
Subject: [PATCH] Recorded work from Tuesday.

---
 divide-and-conquer/notes.md                   | 45 ++++++++++++-------
 .../src/features/median/kthSmallest.js        | 12 ++++-
 2 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/divide-and-conquer/notes.md b/divide-and-conquer/notes.md
index ce6f832..d817da9 100644
--- a/divide-and-conquer/notes.md
+++ b/divide-and-conquer/notes.md
@@ -112,7 +112,7 @@ Problem: Find the median of an odd-length list in average-case Θ(n) time.  Use
 
 ```
             In:  [8, 7, 6, 9, 5], k = 2
-            Out: 7
+            Out: 7 (but how?)
                 /          \
                /            \
     In:  [8, 7], k = 2       In:  [6, 9, 5], k = 0
@@ -131,24 +131,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
+                   j (crossed)
+          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
+          j (crossed)
+          i
+    In:  [5, 7, 6], k = 2
+    Split: [], 5, [7, 6]
+    Out: 7
      |
      |
      |
-    In:  […], k = …
-    In:  […], k = …
-    Split: […], …, […]
-    Out: …
+    In:  [7, 6], k = 2 - 1 (because we threw away one element, 5) = 1
+          *
+                i
+             j (crossed)
+          i
+    In:  [6, 7], k = 1
+    Split: [6], 7, []
+    Out: 7
 ```
 
 ## Analysis
@@ -156,9 +171,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)
diff --git a/divide-and-conquer/src/features/median/kthSmallest.js b/divide-and-conquer/src/features/median/kthSmallest.js
index 7125d1f..0378a9b 100644
--- a/divide-and-conquer/src/features/median/kthSmallest.js
+++ b/divide-and-conquer/src/features/median/kthSmallest.js
@@ -31,5 +31,15 @@ export function kthSmallest(sequence, k) {
     k >= 0 && k < sequence.length,
     'Tried to find the kth smallest element when k is out of bounds.',
   );
-  return sequence[0]; // TODO: stub
+  if (sequence.length < 2) {
+    return sequence[0];
+  }
+  const [left, pivot, right] = partition(sequence);
+  if (k === left.length) {
+    return pivot;
+  }
+  if (k < left.length) {
+    return kthSmallest(left, k);
+  }
+  return kthSmallest(right, k - 1 - left.length);
 }
-- 
GitLab