function linearSearch(images, query) {
return images.filter(img =>
img.name.toLowerCase().includes(query.toLowerCase())
);
}
// Sort by name
images.sort((a, b) => a.name.localeCompare(b.name));
// Sort by date (newest first)
images.sort((a, b) => new Date(b.date) - new Date(a.date));
// Sort by size (largest first)
images.sort((a, b) => b.size - a.size);
function binarySearch(sortedImages, targetName) {
let low = 0, high = sortedImages.length - 1;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
const comparison = sortedImages[mid]
.name.localeCompare(targetName);
if (comparison === 0) return mid;
if (comparison < 0) low = mid + 1;
else high = mid - 1;
}
return -1; // Not found
}
| Dataset Size | Linear O(n) | Binary O(log n) |
|---|---|---|
| 500 items | 500 comparisons | ~9 comparisons |
| 5,000 items | 5,000 comparisons | ~13 comparisons |
| 50,000 items | 50,000 comparisons | ~16 comparisons |
| 1,000,000 items | 1,000,000 comparisons | ~20 comparisons |
Build the complete gallery interface:
| Component | Function |
|---|---|
| Search bar | Live filtering as you type (linear search) |
| Sort dropdown | Name, Date, Size, Type |
| Image grid | Thumbnail + name + metadata |
| Result count | "Showing 12 of 500 images" |
git switch -c feature/PIXELCRAFT-023-gallery-search
git add src/scripts/ src/styles/
git commit -m "Add gallery search and sort with binary search (PIXELCRAFT-023)"
git push origin feature/PIXELCRAFT-023-gallery-search
# PR → Review → Merge → Close ticket ✅
Binary search is O(log n).
Searching 1 million items takes ~20 comparisons. Each step eliminates HALF the remaining options.