Standard Algorithms, Data Structures and Efficiency
IB Computer Science · Computational Thinking, Problem-Solving and Programming
Arrays and collections. An array stores elements of the same type in contiguous memory with a fixed size, and any element can be reached directly from its index in constant time. A collection in the IB sense is accessed sequentially through the operations addItem, getNext, resetNext, hasNext and isEmpty; it can grow, but elements cannot be reached by index. The choice follows from the access pattern: use an array when you need direct access by position or a fixed-size table, and a collection when the number of items is unknown and you only ever process them one after another.
Searching. Linear search examines elements one at a time. It works on unsorted data and on structures that can only be traversed sequentially, needs one comparison in the best case and n in the worst. Binary search repeatedly halves the remaining range, so it needs about log base 2 of n comparisons — around 20 for a million elements — but it requires sorted data and direct access by index. The decision therefore depends on how many searches will be performed: sorting first is only worth it if the cost is spread over many later searches.
Sorting. Bubble sort repeatedly passes through the data swapping adjacent elements that are out of order; it is simple and can stop early on nearly sorted data, but in general it performs on the order of n squared comparisons. Selection sort finds the smallest remaining element on each pass and puts it in place; it always performs the same number of comparisons but far fewer swaps, which matters when moving an element is expensive. Both are quadratic, so both become impractical well before a million elements, which is why real systems use algorithms of order n log n.
Reasoning about efficiency. Count the operations as a function of input size and keep only the dominant term. A single loop over n items is linear; a loop nested inside another is quadratic; halving the problem each step is logarithmic. State which case you are describing, since best, average and worst can differ sharply — bubble sort on already-sorted data with an early-exit check is linear, while its worst case is quadratic. Finally, remember that time is not the only resource: an algorithm can trade memory for speed, and on real hardware an algorithm with worse theoretical complexity can win if it has better locality of reference.
Worked example: When sorting first is worth it, and when it is not
An unsorted list of 10,000 records. Sorting with a quadratic-order algorithm takes about 50 million comparisons. Sequential search needs an average of 5,000 comparisons per search; binary search needs about 14. Determine the number of searches at which sorting first begins to pay off.
Solution
Set up the inequality. Let Q be the number of searches. The no-sorting option costs 5,000 times Q comparisons. The sort-first option costs 50,000,000 plus 14 times Q. Sorting first pays off when 50,000,000 plus 14Q is less than 5,000Q.
Solve. Rearranging: 50,000,000 is less than 4,986Q, so Q is greater than 50,000,000 divided by 4,986, that is, Q is greater than about 10,028. Conclusion: only when performing more than roughly ten thousand searches does sorting first break even.
Read the result correctly, because this is the part that is marked. This figure says that for a database that is looked up constantly — for example a directory searched thousands of times a day — sorting is obviously right and the initial cost is negligible. But for a log file that is searched only a few times and then discarded, sorting is pure waste. The answer is not "binary is faster" but that the cost of sorting must be amortized over the number of searches.
Three factors that change this threshold and should be stated to earn evaluation marks. First, if a sorting algorithm of order n times logarithm of n is used instead of a quadratic one, the sorting cost drops from about 50 million to about 130 thousand comparisons, and the break-even threshold falls to only about 26 searches — so the choice of sorting algorithm changes the conclusion completely. Second, if the data changes frequently, it must be re-sorted or the order must be maintained on insertion, and that cost must be included. Third, if the data can only be traversed sequentially, such as a linked list, then binary search cannot be used whether or not the data is sorted, because it needs direct access by index.
Worked example: Choosing between bubble sort and selection sort using the right criteria
Two situations. (a) Sort a list of 2,000 records where each record is a large block of data, so each swap is very expensive. (b) Re-sort a list of 2,000 numbers that is already almost in order, with only a few elements out of place. Choose an algorithm for each case and explain.
Solution
First, separate two quantities that candidates often merge into one. The number of comparisons and the number of swaps are two different costs, and these two algorithms differ precisely in the second.
Selection sort. Each pass scans the unsorted part to find the smallest element, then swaps it with the element at the start of the unsorted part. The number of comparisons is always of order n squared regardless of the initial data, but the number of swaps is only n minus one, that is, linear — exactly one swap per pass.
Bubble sort. It sweeps through the list and swaps every adjacent pair that is out of order. The number of swaps in the worst case is of order n squared, much higher. In return, if a flag is added to mark "did this pass make any swap" and the algorithm stops when no swaps remain, it needs only a single pass on sorted data — that is, linear order.
(a) Large records, expensive swaps — choose selection sort. Because the cost is dominated by moving data rather than by comparing, the quantity to minimize is the number of swaps. Selection sort performs exactly 1,999 swaps, whereas bubble sort may perform millions. A practical note worth stating: an even better solution is to sort an array of references to the records instead of moving the records themselves, in which case the swap cost becomes negligible and the selection criterion goes back to the number of comparisons.
(b) Almost sorted data — choose bubble sort with an early-stop flag. This is the only case where bubble sort genuinely wins. With nearly ordered data, only a few passes are needed until no pair is out of order and the algorithm stops, giving a cost close to linear order. Selection sort cannot exploit this: it always scans the whole unsorted part on every pass so it still costs quadratic order even when the data is perfectly sorted.
The principle to take away. Do not compare two algorithms with a single number. Ask which cost dominates in this situation — comparing or moving — and does the input have any special property. Those two questions almost always point straight to one choice.
Common mistake
Trap 1 — concluding that binary search is always better. It requires data that is ALREADY SORTED and requires direct access by index. The cost of sorting must be amortized over the number of searches, so with few searches on unsorted data, sequential search wins.Trap 2 — merging the number of comparisons with the number of swaps. Selection sort and bubble sort have the same order for the number of comparisons but differ greatly in the number of swaps, and that is what decides the choice when moving data is expensive.
Trap 3 — talking about efficiency without saying which case is being considered. Bubble sort with an early-stop flag is linear order in the best case and quadratic order in the worst case. If the case is not stated, the answer does not count as correct.