Skip to content

Commit 9901304

Browse files
gabe suggested tweaks
1 parent 0b332f1 commit 9901304

2 files changed

Lines changed: 56 additions & 20 deletions

File tree

src/chapters/sorting-and-binary-search/info-sheet.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ export const sortingBinarySearchInfoSheetChapter = {
1919
2020
## Common Sorting Algorithms
2121
22-
| **Algorithm** | **Best Case** | **Average Case** | **Worst Case** | **Space** |
23-
| ---------------------- | ---------------- | ---------------- | ---------------- | ---------------- |
24-
| **Bubble Sort** | O(n) | O(n²) | O(n²) | O(1) |
25-
| **Selection Sort** | O(n²) | O(n²) | O(n²) | O(1) |
26-
| **Insertion Sort** | O(n) | O(n²) | O(n²) | O(1) |
27-
| **Merge Sort** | O(n log n) | O(n log n) | O(n log n) | O(n) |
28-
| **Quick Sort** | O(n log n) | O(n log n) | O(n²) | O(log n) |
29-
| **Heap Sort** | O(n log n) | O(n log n) | O(n log n) | O(1) |
30-
| **JavaScript .sort()** | O(n log n) | O(n log n) | O(n log n) | O(log n) |
22+
| **Algorithm** | **Best Case** | **Average Case** | **Worst Case** | **Space** | **In Place?** |
23+
| ---------------------- | ---------------- | ---------------- | ---------------- | ---------------- | ---------------- |
24+
| **Bubble Sort** | O(n) | O(n²) | O(n²) | O(1) | Yes |
25+
| **Selection Sort** | O(n²) | O(n²) | O(n²) | O(1) | Yes |
26+
| **Insertion Sort** | O(n) | O(n²) | O(n²) | O(1) | Yes |
27+
| **Merge Sort** | O(n log n) | O(n log n) | O(n log n) | O(n) | No |
28+
| **Quick Sort** | O(n log n) | O(n log n) | O(n²) | O(log n) | Yes |
29+
| **Heap Sort** | O(n log n) | O(n log n) | O(n log n) | O(1) | Yes |
30+
| **JavaScript .sort()** | O(n log n) | O(n log n) | O(n log n) | O(log n) | Yes |
3131
3232
## Key Insights
3333

src/chapters/sorting-and-binary-search/text/sorting-approaches-content.md

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ function bubbleSortAlbums(albums) {
8989

9090
"This method works," Sam explained, "but it'll be really slow when you have a lot of records. I'll have to keep going through the entire collection again and again!"
9191

92+
Here's an animated demonstration of bubble sort: https://csvistool.com/BubbleSort
93+
9294
## Selection Sort: The "Find the Next" Method
9395

9496
"Here's another approach you can use," Maya continued, moving to a different section of unsorted records. "I call it the 'find the next' method. I look through all the remaining unsorted records to find the one that should come next alphabetically, then put it in the correct position."
@@ -125,6 +127,9 @@ Alex watched Maya demonstrate this method. "I can see this is more systematic th
125127

126128
"True," Sam agreed. "But at least I only have to move each record once to its final position, unlike bubble sort where records usually get moved around a lot."
127129

130+
Here's an animated demonstration of selection sort: https://csvistool.com/SelectionSort
131+
This one works top-down, but it's essentially doing the same thing.
132+
128133
## Merge Sort: The "Divide and Conquer" Strategy
129134

130135
Maya stepped in with another suggestion. "Sam, what if you tried a divide-and-conquer approach? Instead of trying to sort your entire collection at once, what if you sorted smaller groups first, then combined them?"
@@ -134,25 +139,43 @@ Sam cocked his head. "...and that helps get it done faster?"
134139
"Yes. This approach is much more efficient for large collections," Maya explained. "Instead of comparing every album with every other album, you're breaking the problem down into smaller, manageable pieces."
135140

136141
```javascript
137-
// Merge sort for album titles
142+
138143
function mergeSortAlbums(albums) {
139144
if (albums.length <= 1) {
140145
return albums;
141146
}
142147

143-
console.log(`Dividing collection of ${albums.length} albums...`);
148+
// Create a copy to avoid modifying the original array
149+
let result = [...albums];
150+
const n = result.length;
144151

145-
// Divide the collection in half
146-
const mid = Math.floor(albums.length / 2);
147-
const left = albums.slice(0, mid);
148-
const right = albums.slice(mid);
152+
console.log(`Starting iterative merge sort of ${n} albums...`);
149153

150-
// Recursively sort both halves
151-
const sortedLeft = mergeSortAlbums(left);
152-
const sortedRight = mergeSortAlbums(right);
154+
// Start with subarrays of size 1, then 2, 4, 8, etc.
155+
for (let size = 1; size < n; size *= 2) {
156+
console.log(`Merging subarrays of size ${size}...`);
157+
158+
// Merge adjacent subarrays of current size
159+
for (let start = 0; start < n; start += size * 2) {
160+
const mid = Math.min(start + size, n);
161+
const end = Math.min(start + size * 2, n);
162+
163+
// Only merge if we have both left and right parts
164+
if (mid < end) {
165+
const left = result.slice(start, mid);
166+
const right = result.slice(mid, end);
167+
const merged = mergeAlbums(left, right);
168+
169+
// Copy merged result back to the main array
170+
for (let i = 0; i < merged.length; i++) {
171+
result[start + i] = merged[i];
172+
}
173+
}
174+
}
175+
}
153176

154-
// Merge the sorted halves
155-
return mergeAlbums(sortedLeft, sortedRight);
177+
console.log("Iterative merge sort complete!");
178+
return result;
156179
}
157180

158181
function mergeAlbums(left, right) {
@@ -181,6 +204,19 @@ function mergeAlbums(left, right) {
181204
}
182205
```
183206

207+
"This merge sort implementation works like organizing a messy pile of albums by using a divide-and-conquer strategy", said Maya, "Imagine you have 8 albums scattered on a table. Rather than trying to sort all 8 at once, we work in stages. First, we look at the albums in pairs and sorts each pair - so now we have 4 sorted pairs of 2 albums each. Then we those sorted pairs and merges them together to create 2 sorted groups of 4 albums each. Finally, we merge those two groups of 4 to get one perfectly sorted collection of all 8 albums. The key insight is that merging two already-sorted groups is much easier and faster than sorting a jumbled mess. Look at the mergeAlbums function. We just compare the first album from each group, pick the one that comes first alphabetically and put it in a result stack, then repeat until we've combined everything."
208+
209+
"That's a lot of steps." mused Alex
210+
211+
"Yes, but the steps are each quick, and each album gets moved exactly the right number of times."
212+
213+
"But I have a lot more than 8 albums," sighed Sam, "Will this still work on a huge collection?"
214+
215+
"Sure. This process can handle any size collection. We keep doubling the group size in each stage until everything is sorted."
216+
217+
Here's an animated demonstration of selection sort: https://csvistool.com/MergeSort
218+
This example works top-down, but it's essentially doing the same thing.
219+
184220
## Real-World Sorting at Groove Records
185221

186222
After trying all three methods, Sam realized each had its place in his record store:

0 commit comments

Comments
 (0)