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

Designed and implemented mergesort and quickmedian.

parent a8f59c86
Branches
No related tags found
No related merge requests found
...@@ -48,19 +48,19 @@ Problem: Sort a list in Θ(n log n) time. ...@@ -48,19 +48,19 @@ Problem: Sort a list in Θ(n log n) time.
``` ```
In: [8, 7, 6, 9, 5] In: [8, 7, 6, 9, 5]
Out: [, …, …, …, …] Out: [5, 6, 7, 8, 9]
/ \ / \
/ \ / \
In: […] In: [] In: [8, 7] In: [6, 9, 5]
Out: […] Out: [] Out: [7, 8] Out: [5, 6, 9]
/ \ / \ / \ / \
/ \ / \ / \ / \
In: [] In: [] In: [] In: […] In: [8] In: [7] In: [6] In: [9, 5]
Out: [] Out: [] Out: [] Out: [] Out: [8] Out: [7] Out: [6] Out: [5, 9]
/ \ / \
/ \ / \
In: [] In: [] In: [9] In: [5]
Out: [] Out: [] Out: [9] Out: [5]
``` ```
## Analysis ## Analysis
...@@ -69,14 +69,14 @@ Out: […] Out: […] Out: […] Out: […] ...@@ -69,14 +69,14 @@ Out: […] Out: […] Out: […] Out: […]
T(n) = T(⌊n/2⌋) + T(⌈n/2⌉) + Θ(n) for n ≫ 0 T(n) = T(⌊n/2⌋) + T(⌈n/2⌉) + Θ(n) for n ≫ 0
T(n) ≈ 2T(n/2) + Θ(n) for n ≫ 0 T(n) ≈ 2T(n/2) + Θ(n) for n ≫ 0
T(n) = Θ() T(n) = Θ(n log n)
* Uneven split: * Uneven split:
T(n) = T(n - 1) + T(1) + Θ(n) for n ≫ 0 T(n) = T(n - 1) + T(1) + Θ(n) for n ≫ 0
T(n) = T(n - 1) + Θ(1) + Θ(n) for n ≫ 0 T(n) = T(n - 1) + Θ(1) + Θ(n) for n ≫ 0
T(n) = T(n - 1) + Θ(n) for n ≫ 0 T(n) = T(n - 1) + Θ(n) for n ≫ 0
T(n) = Θ() T(n) = Θ()
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
...@@ -96,11 +96,11 @@ Problem: Find the median of an odd-length list in average-case Θ(n) time. Use ...@@ -96,11 +96,11 @@ Problem: Find the median of an odd-length list in average-case Θ(n) time. Use
``` ```
In: [8, 7, 6, 9, 5] In: [8, 7, 6, 9, 5]
Out: Out: ????
/ \ / \
/ \ / \
In: […] In: [] In: [8, 7] In: [6, 9, 5]
Out: Out: 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. * Option A: We are missing information from the recursive calls. Add output(s) so that they can return that information.
...@@ -108,15 +108,15 @@ Problem: Find the median of an odd-length list in average-case Θ(n) time. Use ...@@ -108,15 +108,15 @@ Problem: Find the median of an odd-length list in average-case Θ(n) time. Use
## Design (second attempt) ## 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 In: [8, 7, 6, 9, 5], k = 2
Out: … Out: …
/ \ / \
/ \ / \
In: [], k = 2 In: [], k = 0 In: [8, 7], k = 2 In: [6, 9, 5], k = 0
Out: Out: Out: ???? Out: 5
``` ```
## Partitioning ## Partitioning
...@@ -131,24 +131,39 @@ Problem: Find the median of an odd-length list in average-case Θ(n) time. Use ...@@ -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: [8, 7, 6, 9, 5], k = 2
In: […], k = 2 *
In: […], k = 2 i
Split: […], …, […] j
Out: … 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: [5, 7, 6], k = 2
In: […], k = … *
Split: […], …, […] i (crossed)
Out: … j
i
In: [5, 7, 6], k = 2
Split: [], 5, [7, 6]
Out: 7
| |
| |
| |
In: […], k = … In: [7, 6], k = 1
In: […], k = … *
Split: […], …, […] i (crossed)
Out: … j
i
In: [6, 7], k = 1
Split: [6], 7, []
Out: 7
``` ```
## Analysis ## Analysis
...@@ -156,9 +171,9 @@ Problem: Find the median of an odd-length list in average-case Θ(n) time. Use ...@@ -156,9 +171,9 @@ Problem: Find the median of an odd-length list in average-case Θ(n) time. Use
* Uneven split: * Uneven split:
T(n) = T(n - 1) + Θ(n) for n ≫ 0 T(n) = T(n - 1) + Θ(n) for n ≫ 0
T(n) = Θ() T(n) = Θ()
* Even split: * Even split:
T(n) ≈ T(n/2) + Θ(n) for n ≫ 0 T(n) ≈ T(n/2) + Θ(n) for n ≫ 0
T(n) = Θ() T(n) = Θ(n)
...@@ -31,5 +31,15 @@ export function kthSmallest(sequence, k) { ...@@ -31,5 +31,15 @@ export function kthSmallest(sequence, k) {
k >= 0 && k < sequence.length, k >= 0 && k < sequence.length,
'Tried to find the kth smallest element when k is out of bounds.', '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 - left.length - 1);
} }
function merge(left, right) { function merge(left, right) {
return []; // TODO: stub const results = [];
let i = 0;
let j = 0;
while (i < left.length || j < right.length) {
if (j >= right.length || (i < left.length && left[i] < right[j])) {
results.push(left[i]);
++i;
} else {
results.push(right[j]);
++j;
}
}
return results;
} }
export function sort(list) { export function sort(list) {
return list; // TODO: stub if (list.length < 2) {
return list;
}
const middleIndex = Math.floor(list.length / 2);
const left = sort(list.slice(0, middleIndex));
const right = sort(list.slice(middleIndex, list.length));
return merge(left, right);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment