NivaarExam PrepOfficial exam papers ↗

25-Comp-A4 Program Design and Data Structures · May 2013

Question 8 of 8: Algorithm Design and Sorting

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)

Notes on this paper

National Exams — May 2013 — 98-Comp-A4 Program Design and Data Structures. Three-hour, closed-book exam, no calculator permitted. Format: eight questions, candidates answer any five (all questions equal weight; only the first five appearing in the answer book are marked). Pseudocode or a high-level language (C or C++) is acceptable throughout — marking emphasizes program operation, not syntactic detail. All eight questions are solved below for completeness. No marks breakdown per sub-part is given on the source paper beyond the "equal weight" instruction.

Reference texts: Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms (3rd ed., MIT Press) — algorithm design, complexity analysis, sorting; Weiss, Data Structures and Algorithm Analysis in C (2nd ed., Pearson) — linked lists, stacks, pointer-based structures; Deitel & Deitel, C++ How to Program (9th ed., Pearson) — classes, templates, operator overloading; Kernighan & Ritchie, The C Programming Language (2nd ed.) — file I/O and arrays.

Question 8: Algorithm Design and Sorting

Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.

Given. An unsorted array $A$ of $n$ integers, possibly with duplicates, $n$ large. Find. Which of the two proposed methods for building the duplicate-free array $B$ is asymptotically faster, and why.

Approach. Analyze the dominant cost of each method as a function of $n$: Method 1's cost is driven by how the "does it already exist in B?" check scales as $B$ grows; Method 2's cost is dominated by the sort, but a sorted array turns the existence check into a trivial adjacent comparison.

  1. Method 1: linear existence check. For each of the $n$ elements of $A$, checking "does it already exist in $B$?" by scanning $B$ costs, in the worst case (few or no duplicates, so $B$ grows to nearly size $n$), up to $|B|$ comparisons. Summed over all $n$ insertions this is $$1+2+\cdots+(n-1) = \frac{n(n-1)}{2} = \boxed{O(n^2)}.$$
  2. Method 2: sort, then a single dedup pass. Quicksort costs $O(n\log n)$ on average (its worst case, $O(n^2)$, only arises on already-sorted or adversarial pivot-selection inputs, and is avoided in practice with randomized or median-of-three pivot choice). Once $A$ is sorted, every duplicate of a value is adjacent to it, so a single linear scan comparing each element only to its immediate predecessor removes all duplicates in $O(n)$. Total: $$O(n\log n) + O(n) = \boxed{O(n\log n)}\ \text{(average case)}.$$
  3. Compare for large $n$. Because $n\log n$ grows strictly slower than $n^2$ once $n$ is at all large, Method 2 wins asymptotically — and the crossover happens quickly: at $n=1000$, $n^2 \approx 5\times$ the operation count of $n\log n$ already, and the gap widens without bound as $n$ grows further.
MethodDominant costComplexity
1: linear existence check while building Bup to $|B|$ comparisons per insertion$O(n^2)$ worst case (few duplicates)
2: Quicksort, then adjacent-duplicate scanthe sort dominates; dedup pass is $O(n)$$O(n\log n)$ average case
Winner for large $n$$\boxed{\text{Method 2}}$ — $n\log n$ beats $n^2$ once $n$ is large
Back to the paper →