Skip to content
Snippets Groups Projects
Commit 369965d5 authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Designed and traced quickmedian.

parent b6aea74c
No related branches found
No related tags found
No related merge requests found
......@@ -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) = Θ()
* Even split:
T(n) ≈ T(n/2) + Θ(n) for n ≫ 0
T(n) = Θ()
T(n) = Θ(n)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment