Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
Divide and Conquer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CSCE 310
Divide and Conquer
Commits
3ae41e70
Commit
3ae41e70
authored
1 year ago
by
Brady James Garvin
Browse files
Options
Downloads
Patches
Plain Diff
Recorded work from Monday.
parent
19a919ea
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
divide-and-conquer/notes.md
+33
-14
33 additions, 14 deletions
divide-and-conquer/notes.md
divide-and-conquer/src/features/median/kthSmallest.js
+11
-1
11 additions, 1 deletion
divide-and-conquer/src/features/median/kthSmallest.js
with
44 additions
and
15 deletions
divide-and-conquer/notes.md
+
33
−
14
View file @
3ae41e70
...
...
@@ -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
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 = 1
*
i
j (crossed)
i
In: [6, 7], k = 1
Split: [6], 7, []
Out: 7
```
## Analysis
...
...
@@ -129,9 +144,13 @@ 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) = "about" Θ(1) + Θ(n) = Θ(n)
*
Land on the pivot:
T(n) = 0 + Θ(n) = Θ(n)
This diff is collapsed.
Click to expand it.
divide-and-conquer/src/features/median/kthSmallest.js
+
11
−
1
View file @
3ae41e70
...
...
@@ -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
-
left
.
length
-
1
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment