Skip to main content

Module 2: Sorting, Searching & Fundamental Structures

Primary texts: The Algorithm Design Manual (Skiena) for problem-driven intuition and Introduction to Algorithms (CLRS) for proofs and cost analysis
Selective support: Algorithms (Sedgewick) for implementation texture; Grokking Algorithms for first-pass framing; Competitive Programming for exercise volume

This guide is the primary teacher. You do not need to read the source books front-to-back to complete this module. You do need to become fluent in sorting, searching, hashing, and heap-based priority queues -- both as algorithms and as a vocabulary for expressing cost tradeoffs.


Scope of This Module​

This module is not a catalog of every sort ever invented. It is where you learn to reason about why a particular operation costs what it does and when to pay that cost.

What it covers in depth:

  • insertion, merge, and quicksort as contrasting comparison-sort designs
  • the Omega(n log n) lower bound proved through decision trees
  • linear-time specialty sorts (counting, radix) and the assumptions they pay for their speed with
  • external sorting resource accounting and verification of library contracts
  • binary search as an invariant-driven template, not a trick
  • selection and order statistics in linear time (quickselect, median-of-medians)
  • arrays vs linked lists vs trees vs hash tables as a choice about access patterns
  • hashing as a randomized model: expected O(1), universal hashing, load factor, collision resolution
  • binary heaps as implicit trees and the priority-queue API they enable
  • heapsort and the pattern-by-pattern use of priority queues in simulation, Dijkstra, top-K, median maintenance, and k-way merge

What it deliberately does not try to finish here:

  • balanced search trees beyond a mention (Module 5 handles them)
  • graph algorithms beyond naming Dijkstra as a PQ client (Module 3)
  • advanced amortized data structures (Module 5); reuse Module 1 analysis for resizing here
  • advanced heaps such as Fibonacci heaps (Module 5)

This is a foundations module. If you can implement these algorithms but cannot explain their cost profiles in a sentence, you are not done.


Before You Start​

Complete the Prerequisite Diagnostic: tracing, induction, recurrences, expectation, and record identity. Repair failed prerequisites before the relevant cluster. Sorting stability, heaps and deletion are taught here; they are not required prior knowledge.

Budget 65–90 focused hours, roughly 7–9 weeks at 10 hours per week. This is an author estimate pending learner pilots. Prior experience may shorten the path; mastery evidence, not elapsed time, determines completion.


What This Module Is For​

Sorting, searching, hashing, and priority queues are the plumbing of almost every non-trivial algorithm. Later work repeatedly asks:

  • how do I find one element or one order statistic out of many?
  • how do I organize data so that insert, lookup, and delete are all fast?
  • how do I keep track of the next-most-important item in a dynamic collection?
  • which algorithm costs what on which input distribution?
  • when does a library default hide a performance or correctness trap?

This module trains the reasoning needed for:

  • graph algorithms (Module 3) that use PQs for Dijkstra, Prim, A*
  • dynamic programming (Module 4) that often needs sorted input or fast lookup
  • advanced structures (Module 5) that generalize the "tree in an array" and amortization ideas introduced here
  • systems, database, and compiler work that depend on cache-aware sorting and hashing

You are learning to reason about data-handling cost precisely and pick the right tool for a job.


Concept Map​


How To Use This Module​

Work in order. Later clusters depend on earlier habits -- in particular the invariant-writing reflex from Cluster 1 and the probability-of-collision reasoning from Cluster 4.

Cluster 1: Comparison-Based Sorting and the Lower Bound​

OrderConceptTypeFocus
1Insertion Sort Teaches Lessons That SurviveSUPPORTINGInvariants, adaptivity, and why library sorts switch to insertion sort on small subarrays
2Mergesort Is the Divide-and-Conquer ExemplarPRIMARYRecurrence T(n) = 2T(n/2) + Theta(n), stability, and guaranteed O(n log n)
3Quicksort and Why Randomization HelpsPRIMARYPartition, expected O(n log n), pivot choice, adversarial vs unlucky inputs
4Comparison Sorting Has an Omega(n log n) Lower BoundPRIMARYDecision trees, log2(n!), and what counts as a "comparison"

Cluster mastery check: Can you write a loop invariant, derive Theta(n log n) from the mergesort recurrence, and explain why no comparison-based sort can do asymptotically better?

Cluster 2: Linear-Time and Specialized Sorting​

OrderConceptTypeFocus
5Counting Sort and When Integer Keys Break the Lower BoundSUPPORTINGTheta(n + R) stable record sorting, range width, and comparison-model assumptions
6Radix Sort and Linear in Key WidthSUPPORTINGLSD vs MSD, the real meaning of "linear," and how radix plugs into counting sort
7External and Practical SortingSUPPORTINGLibrary contracts, memory budgets, run generation, merge passes, and I/O accounting

Cluster mastery check: Can you pick the right sort given the input shape, key structure, and memory budget -- and justify why the textbook asymptotics alone do not decide?

Cluster 3: Searching in Sorted and Unsorted Data​

OrderConceptTypeFocus
8Binary Search and Its InvariantPRIMARYThe correctness template that generalizes to lower_bound, upper_bound, and predicate-based search
9Selection and Order Statistics in O(n)PRIMARYQuickselect, median-of-medians, and the partition primitive as a general tool
10Search-Data-Structure ChoiceSUPPORTINGArray vs linked list vs balanced BST vs hash table, by access pattern

Cluster mastery check: Can you state the invariant for a binary search before writing any code, and can you match a storage shape to its operation-cost profile?

Cluster 4: Hash Tables as a Dictionary Primitive​

OrderConceptTypeFocus
11Hashing as a Randomized ModelPRIMARYExpected O(1) as a probabilistic guarantee, and what breaks it
12Collision Resolution: Chaining vs Open AddressingPRIMARYTwo schemes, their load-factor behavior, and deletion traps
13Universal Hashing, Load Factor, and ResizingSUPPORTINGUniversal families, cryptographic vs non-cryptographic hashing, amortized resizes

Cluster mastery check: Can you explain why hash-table O(1) is expected rather than worst case, and can you distinguish a fixed-key random-hash guarantee from adversarial behavior?

Cluster 5: Heaps and Priority Queues​

OrderConceptTypeFocus
14The Binary Heap as an Implicit TreePRIMARYArray-backed heap layout, sift-up/down, and the O(n) build argument
15Heapsort and the Priority-Queue APIPRIMARYIn-place heapsort, identity, stable ties, and indexed versus lazy updates
16Priority-Queue ApplicationsSUPPORTINGEvent simulation, Dijkstra relaxation, top-K, median maintenance, k-way merge

Cluster mastery check: Can you spot a problem whose loop is "pick the best remaining element, do work, possibly produce new elements," and translate it to a heap-backed priority queue?

Then work these practice pages:

OrderPractice pathFocus
1Sorting Correctness and Analysis LabInvariants, complexity derivations, stability, and complexity-distinguishing drills
2Searching and Selection WorkshopBinary search variants, quickselect, predicate-based search
3Hashing and Priority-Queue ClinicCollision reasoning, universal hashing, heap operations, PQ applications
4Implementation and Transfer StudioResource budgets, event identity, integer representations, and streaming transfer

Use Module Quiz after the concept and practice path. Use Reference and Selective Reading and Learning Resources only for targeted reinforcement.


Learning Objectives​

By the end of this module you should be able to:

  1. Derive insertion-sort and partition invariants, and implement stable mergesort independently.
  2. Derive the Theta(n log n) cost of mergesort and the expected O(n log n) of randomized quicksort.
  3. Prove the Omega(n log n) comparison-sort lower bound via decision trees.
  4. Derive stable counting and radix passes with explicit representation costs; implement them on the integer-sorting extension path.
  5. Choose the right sorting algorithm given input shape, key structure, memory budget, and stability needs.
  6. Implement binary search and its variants (lower_bound, upper_bound, predicate-based) using the invariant template.
  7. Implement quickselect and describe when linear-time selection beats sorting.
  8. Implement linear probing with correct deletion and updates, compare it with chaining, and qualify load-factor and hashing guarantees.
  9. Implement a binary-heap-backed priority queue with insert, extract_min, peek, and understand how decrease_key is typically supported.
  10. Identify priority-queue patterns in real problems: simulation, shortest path, top-K, median maintenance, k-way merge.

Outputs​

  • Six runnable components across four labs: stable merge sort, equal range, selection, a map, a heap, and a streamed merge.
  • Sixteen practice problems with derivations, traces, counterexamples, and feedback from the separate solutions.
  • One evidence document separating proofs, finite checks, measured behavior, and assumptions; retain actual mistakes and repairs without an arbitrary quota.
  • One event-pipeline capstone, including a changed requirement and honest accounting for dictionary state as well as merge state.
  • One independent eight-competency assessment and a delayed fresh transfer task.

Completion Standard​

Use Competencies, Evidence, and Remediation. Require at least level 2 in every competency, passing lab contracts, source review for complexity, and an independent explanation. Repair critical errors and attempt fresh tasks. Do not equate first-try memorized code with mastery or average away a missing skill.

Use Contracts, Representation, and Performance to qualify implementation and benchmarking claims. Extend with stable integer sorting, BFPRT, cancellation, or buffered external merge after the core requirements pass.


Reading Policy​

  • Concept pages are the main path.
  • Local book chunks are selective reinforcement, not a second syllabus.
  • Read only if stuck means try the concept page, self-check, and drill first.
  • Optional deep dive means additional nuance or exercise volume, not required progression.
  • Primary external sources are collected in resources.md; concept pages link to course book indexes and bounded assignments.

Suggested Study Sequence​

StageFocusEstimated focused hours
Entry and conceptsDiagnostic, 16 lessons, targeted book reinforcement, engineering contracts24–32
Guided implementationsFour labs with proofs, failures and repairs14–20
Independent practiceP1–P16 and solution review14–20
Assessment and remediationIndependent attempt, review, fresh rechecks and delayed transfer7–10
IntegrationEvent-pipeline capstone and changed requirement6–8

Total: 65–90 hours. Pause for remediation where needed. The optional depth path is additional work; it should answer a specific engineering question.


Reference​

For book indexes, topic assignments, and the local collection map, use Reference and Selective Reading.


Rich Learning Pages​

Worked Examples | Guided Labs | Case Studies | Mistake Clinic | Reading Guide | Capstone Thread


Model Artifact Calibration​

For benchmark calibration, compare your measurements and decision writeup to the algorithm benchmark note model artifact.

Assessment and Review Navigation​

Prerequisite Diagnostic | Engineering Contracts | Practice Solutions | Assessment Rubric | Assessment Solutions | Instructor Review