You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/chapters/sorting-and-binary-search/text/sorting-approaches-content.md
+47-11Lines changed: 47 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -89,6 +89,8 @@ function bubbleSortAlbums(albums) {
89
89
90
90
"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!"
91
91
92
+
Here's an animated demonstration of bubble sort: https://csvistool.com/BubbleSort
93
+
92
94
## Selection Sort: The "Find the Next" Method
93
95
94
96
"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
125
127
126
128
"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."
127
129
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
+
128
133
## Merge Sort: The "Divide and Conquer" Strategy
129
134
130
135
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?"
134
139
"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."
135
140
136
141
```javascript
137
-
// Merge sort for album titles
142
+
138
143
functionmergeSortAlbums(albums) {
139
144
if (albums.length<=1) {
140
145
return albums;
141
146
}
142
147
143
-
console.log(`Dividing collection of ${albums.length} albums...`);
148
+
// Create a copy to avoid modifying the original array
149
+
let result = [...albums];
150
+
constn=result.length;
144
151
145
-
// Divide the collection in half
146
-
constmid=Math.floor(albums.length/2);
147
-
constleft=albums.slice(0, mid);
148
-
constright=albums.slice(mid);
152
+
console.log(`Starting iterative merge sort of ${n} albums...`);
149
153
150
-
// Recursively sort both halves
151
-
constsortedLeft=mergeSortAlbums(left);
152
-
constsortedRight=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
+
constmid=Math.min(start + size, n);
161
+
constend=Math.min(start + size *2, n);
162
+
163
+
// Only merge if we have both left and right parts
164
+
if (mid < end) {
165
+
constleft=result.slice(start, mid);
166
+
constright=result.slice(mid, end);
167
+
constmerged=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
+
}
153
176
154
-
// Merge the sorted halves
155
-
returnmergeAlbums(sortedLeft, sortedRight);
177
+
console.log("Iterative merge sort complete!");
178
+
returnresult;
156
179
}
157
180
158
181
functionmergeAlbums(left, right) {
@@ -181,6 +204,19 @@ function mergeAlbums(left, right) {
181
204
}
182
205
```
183
206
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
+
184
220
## Real-World Sorting at Groove Records
185
221
186
222
After trying all three methods, Sam realized each had its place in his record store:
0 commit comments