Why Not Sort Only When We Search?
Keeping the array sorted at all times is what makes SortedArray’s add expensive. We could leave the array unsorted, keep the adds, and sort the array only when we actually need to search it.
Now that we have the notion of Big-Oh, we can analyze that idea. Let’s say we have an array of elements. We want to search it once. The plan is to sort it first and then binary search it. What is the Big-Oh of that plan?
The selection sort we analyzed earlier is . There are faster sorting algorithms that run in time, and we will study them later in the course. Let’s use the faster figure, , so the idea gets the lowest sorting cost available. Even then, sorting an array costs more than a single linear scan of it, which is .
Sorting the array for a single search is worse than not sorting at all. So when is sort-on-demand worth it? Only if we sort once and then search many times, with no changes to the array in between. Then the is paid once and shared across all those searches, and each search after the first is only . This is the same trade-off SortedArray makes. The only difference is when the sorting cost is paid. A SortedArray pays it a little at a time, on every add. Sort-on-demand keeps adds at and pays all at once, the next time someone searches after a change.
Which of those is better depends on the workload again. This time what matters is whether the adds and the searches are interleaved or come in bursts. If the data arrives in bulk and is then searched many times, sort once after it is loaded and you pay once. If adds and searches alternate, sort-on-demand is the worse choice, because every search that follows an add has to sort the whole array again. In that case keeping the array sorted on every add is better.