Step 5 of 6
Searching and sorting
Binary search finds an item in a sorted list by halving it each step: 1 million items takes about 20 checks. Merge sort and quicksort sort in O(n log n). You'll rarely write a sort yourself, but you should know what .sort() costs and that JavaScript's default sort compares numbers as text.
Why it matters when you build with AI: [10, 9, 1].sort() gives [1, 10, 9] in JavaScript. AI code hits this bug constantly.
Quick check
In JavaScript, [10, 9, 1].sort() returns…
Default sort compares as text. Use .sort((a, b) => a - b).