Skip to content

Commit 6c195d5

Browse files
authored
Merge pull request #55 from NSS-Workshops/sort_search
hotfixes: binary search variants example and removed checkpoint
2 parents b0a3242 + abb1ab8 commit 6c195d5

10 files changed

Lines changed: 199 additions & 116 deletions

src/chapters/sorting-and-binary-search/binary-search-fundamentals.js renamed to src/chapters/sorting-and-binary-search/binary-search-fundamentals.jsx

File renamed without changes.

src/chapters/sorting-and-binary-search/binary-search-variants.js

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import Checkpoint, { QUESTION_TYPES } from "../../components/Checkpoint";
2+
import content from './text/binary-search-variants-content.md?raw';
3+
import example from './text/binary-search-variants-example.js?raw';
4+
import completed from './text/binary-search-variants-completed.js?raw';
5+
6+
const questions = [
7+
{
8+
type: QUESTION_TYPES.RADIO,
9+
questionJsx:<p>When using binary search to find the FIRST occurrence of a target value in a sorted array with duplicates, what should you do when you find a match?</p>,
10+
answers: [
11+
"Return the index immediately since you found the target",
12+
"Continue searching in the right half to find more occurrences",
13+
"Save the current index as a potential result and continue searching in the left half",
14+
"Check both left and right halves simultaneously"
15+
],
16+
correctAnswer: 2
17+
},
18+
{
19+
type: QUESTION_TYPES.RADIO,
20+
questionJsx:<p>What does the "insertion point" variant of binary search help you find?</p>,
21+
answers: [
22+
"The exact location where a duplicate element exists",
23+
"The position where a new element should be inserted to maintain sorted order",
24+
"The middle element of any array",
25+
"The last occurrence of a target element"
26+
],
27+
correctAnswer: 1
28+
}
29+
];
30+
31+
export const binarySearchVariantsChapter = {
32+
id: 'binary-search-variants',
33+
title: 'Binary Search Variants',
34+
sectionId: 'sorting-and-binary-search',
35+
previousChapterId: 'binary-search-fundamentals',
36+
content: content,
37+
exercise: {
38+
starterCode: example,
39+
solution: completed,
40+
question: `
41+
# Exercise Question
42+
`,
43+
solution_explanation: `
44+
# Solution Explanation
45+
it goes like this
46+
`
47+
},
48+
quiz: {
49+
component: () => <>
50+
<h1>Checkpoint</h1>
51+
<Checkpoint questions={questions}/>
52+
</>
53+
}
54+
};
Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,51 @@
1-
import { useAutoGradeQuiz } from "../../components/useAutoGradeQuiz";
1+
import Checkpoint, { QUESTION_TYPES } from "../../components/Checkpoint";
2+
3+
const questions = [
4+
{
5+
type: QUESTION_TYPES.RADIO,
6+
questionJsx: <p>What are the two essential requirements for binary search to work effectively?</p>,
7+
answers: [
8+
"The array must be sorted and contain only unique elements",
9+
"The array must be large and stored in memory",
10+
"The array must be sorted and support random access (O(1) indexing)",
11+
"The array must be numeric and have no null values"
12+
],
13+
correctAnswer: 2
14+
},
15+
{
16+
type: QUESTION_TYPES.RADIO,
17+
questionJsx: <p>What is the time complexity of binary search, and why is it that efficient?</p>,
18+
answers: [
19+
"O(n) because it checks every element once",
20+
"O(log n) because it eliminates half the remaining elements with each comparison",
21+
"O(n²) because it compares elements in nested loops",
22+
"O(1) because it finds elements instantly"
23+
],
24+
correctAnswer: 1
25+
},
26+
{
27+
type: QUESTION_TYPES.RADIO,
28+
questionJsx: <p>What happens when you call `array.sort()` on an array of numbers like `[1.99, 15.99, 100.99, 20.99, 3.99]` without providing a comparator function?</p>,
29+
answers: [
30+
"It sorts numerically: `[1.99, 3.99, 15.99, 20.99, 100.99]`",
31+
"It sorts as strings: `[1.99, 100.99, 15.99, 20.99, 3.99]`",
32+
"It throws an error because numbers need a comparator",
33+
"It returns the array unchanged"
34+
],
35+
correctAnswer: 1
36+
},
37+
{
38+
type: QUESTION_TYPES.RADIO,
39+
questionJsx: <p>In a JavaScript comparator function with parameters a and b, what should you return if element a should come BEFORE element b in the sorted order?</p>,
40+
answers: [
41+
"false",
42+
"true",
43+
"A positive number",
44+
"A negative number"
45+
],
46+
correctAnswer: 3
47+
},
48+
];
249

350
export const sortingAndBinarySearchCheckpointChapter = {
451
id: 'sorting-and-binary-search-checkpoint',
@@ -11,27 +58,7 @@ export const sortingAndBinarySearchCheckpointChapter = {
1158
Test your understanding of sorting algorithms and binary search.
1259
`,
1360
quiz: {
14-
component: () => {
15-
const CheckpointComponent = () => {
16-
useAutoGradeQuiz();
17-
18-
return (
19-
<main>
20-
<h2>Quiz: Sorting and Binary Search</h2>
21-
<form className="auto-graded-quiz">
22-
<div className="question" data-answer="">
23-
<p>Question placeholder - to be added later</p>
24-
<input type="text" required />
25-
<span className="feedback" />
26-
</div>
27-
28-
<button className="code-button test-button" type="submit">Submit</button>
29-
</form>
30-
</main>
31-
);
32-
};
33-
34-
return <CheckpointComponent />;
35-
}
61+
component: () => <Checkpoint questions={questions}/>
3662
}
37-
};
63+
};
64+

src/chapters/sorting-and-binary-search/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { sortingAndBinarySearchLearningObjectivesChapter } from './learning-objectives';
2-
import { sortingApproachesChapter } from './sorting-approaches';
3-
import { sortMethodChapter } from './sort-method';
4-
import { binarySearchFundamentalsChapter } from './binary-search-fundamentals';
5-
import { binarySearchVariantsChapter } from './binary-search-variants';
2+
import { sortingApproachesChapter } from './sorting-approaches.jsx';
3+
import { sortMethodChapter } from './sort-method.jsx';
4+
import { binarySearchFundamentalsChapter } from './binary-search-fundamentals.jsx';
5+
import { binarySearchVariantsChapter } from './binary-search-variants.jsx';
66
import { sortingBinarySearchInfoSheetChapter } from './info-sheet';
77
import { supplementalMaterialsChapter } from './supplemental-materials';
88
import { glossaryChapter } from './glossary';

src/chapters/sorting-and-binary-search/sort-method.js

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import Checkpoint, { QUESTION_TYPES } from "../../components/Checkpoint";
2+
import content from './text/sort-method-content.md?raw';
3+
import example from './text/sort-method-example.js?raw';
4+
import completed from './text/sort-method-completed.js?raw';
5+
6+
const questions = [
7+
{
8+
type: QUESTION_TYPES.RADIO,
9+
questionJsx:<p>When implementing multi-level sorting (e.g., sort by artist, then by year within each artist), what is the correct approach in your comparator function?</p>,
10+
answers: [
11+
"Always sort by the primary criterion only",
12+
"Compare the primary criterion first; if equal, then compare the secondary criterion",
13+
"Sort by secondary criterion first, then primary criterion",
14+
"Use separate sort() calls for each criterion"
15+
],
16+
correctAnswer: 1
17+
}
18+
];
19+
20+
export const sortMethodChapter = {
21+
id: 'sort-method',
22+
title: 'Using .sort() with Comparators',
23+
sectionId: 'sorting-and-binary-search',
24+
previousChapterId: 'sorting-approaches',
25+
content: content,
26+
exercise: {
27+
starterCode: example,
28+
solution: completed,
29+
question: `
30+
# Exercise Question
31+
`,
32+
solution_explanation: `
33+
# Solution Explanation
34+
it goes like this
35+
`
36+
},
37+
quiz: {
38+
component: () => <>
39+
<h1>Checkpoint</h1>
40+
<Checkpoint questions={questions}/>
41+
</>
42+
}
43+
};

src/chapters/sorting-and-binary-search/sorting-approaches.js

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Checkpoint, { QUESTION_TYPES } from "../../components/Checkpoint";
2+
import content from './text/sorting-approaches-content.md?raw';
3+
4+
const questions = [
5+
{
6+
type: QUESTION_TYPES.RADIO,
7+
questionJsx:<p>Which sorting algorithm has O(n log n) time complexity in ALL cases (best, average, and worst) and is stable (preserves relative order of equal elements)?</p>,
8+
answers: [
9+
"Bubble Sort",
10+
"Selection Sort",
11+
"Merge Sort",
12+
"Quick Sort"
13+
],
14+
correctAnswer: 2
15+
}
16+
];
17+
18+
export const sortingApproachesChapter = {
19+
id: 'sorting-approaches',
20+
title: 'Common Sorting Approaches: Bubble, Selection, Merge',
21+
sectionId: 'sorting-and-binary-search',
22+
previousChapterId: 'sorting-binary-search-learning-objectives',
23+
content: content,
24+
exercise: null,
25+
quiz: {
26+
component: () => <>
27+
<h1>Checkpoint</h1>
28+
<Checkpoint questions={questions}/>
29+
</>
30+
}
31+
};

src/chapters/sorting-and-binary-search/text/binary-search-variants-example.js

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,28 @@
11
function findClosestElement(arr, target) {
2-
if (arr.length === 0) {
3-
return -1;
4-
}
2+
3+
//if the array is empty, return -1
54

6-
// If target is less than the smallest element
7-
if (target <= arr[0]) {
8-
return 0;
9-
}
5+
// If target is less than the smallest element, return zero
6+
7+
// If target is greater than the largest element, return the index of the last element
108

11-
// If target is greater than the largest element
12-
if (target >= arr[arr.length - 1]) {
13-
return arr.length - 1;
14-
}
9+
//set left to 0 and right to the index of the last element
1510

16-
let left = 0;
17-
let right = arr.length - 1;
11+
//while left is less than or equal to right...
1812

19-
while (left <= right) {
20-
const mid = Math.floor(left + (right - left) / 2);
13+
//find the midpoint index
14+
//if this is an exact match, return this index
2115

22-
// Found exact match
23-
if (arr[mid] === target) {
24-
return mid;
25-
}
16+
// If target is less than element at midpoint, set right to just below mid to search left half
2617

27-
// If target is less than mid, search left half
28-
if (arr[mid] > target) {
29-
right = mid - 1;
30-
}
31-
// If target is greater than mid, search right half
32-
else {
33-
left = mid + 1;
34-
}
35-
}
18+
// otherwise, set left to just above midpoint to search right half\
19+
20+
//end while loop
21+
3622

3723
// At this point, left > right
38-
// Compare the closest elements on both sides
39-
if (Math.abs(arr[left] - target) < Math.abs(arr[right] - target)) {
40-
return left;
41-
} else {
42-
return right;
43-
}
24+
// Compare the closest elements on both sides and return whichever is closer (absolute value of the difference)
25+
4426
}
4527

4628
// Example

0 commit comments

Comments
 (0)