Advanced Topics in Computer Science

Unit 2: Sorting, Searching, and Algorithm Efficiency

Students compare algorithms by correctness, runtime, memory, and implementation complexity, with sorting and searching as the main laboratory.

Unit Focus

What this unit is really teaching

This unit builds the Big-O vocabulary needed for data structures, competitive programming, and college CS.

Key Topics

  • +Linear search, binary search, and preconditions for efficient lookup
  • +Selection sort, insertion sort, merge sort, quicksort ideas, and heap sort preview
  • +Best, average, and worst-case runtime reasoning
  • +Big-O, Big-Omega, and Big-Theta intuition at an age-appropriate level
  • +Empirical timing experiments and input-size growth
  • +Stability, in-place sorting, recursion depth, and memory tradeoffs

Practice Work

Implementation and analysis tasks

Student task

Benchmark several search and sort algorithms on random, sorted, and nearly sorted data.

Student task

Create an algorithm comparison report with traces and runtime tables.

DSA Assignment Bank

30 interview-style algorithm questions for this unit

Code Scholars provides structured assignments, implementation support, edge-case review, and runtime analysis coaching so students learn the concept rather than only memorizing a solution.

1

Binary search on answer

Find first true in a sorted condition

Given a monotonic condition over integer values, find the smallest value where the condition becomes true.

2

Binary search variants

Locate first and last occurrence

Given a sorted array with duplicates, return the first and last index of a target without scanning the whole range.

3

Binary search with cases

Search a rotated sorted array

Find a target in a rotated sorted array by identifying which half is ordered at each step.

4

Lower-bound search

Find insertion position

Return the index where a target should be inserted to keep a sorted array ordered, including duplicates.

5

Sort then scan

Merge overlapping time intervals

Sort meeting intervals by start time and merge any intervals that overlap or touch.

6

Greedy sorting

Schedule maximum non-overlapping tasks

Choose the largest number of tasks that can fit without overlap by sorting by ending time.

7

Heap or insertion reasoning

Sort nearly sorted activity logs

Given logs where each item is at most k positions away from its correct spot, choose and justify an efficient sorting approach.

8

Selection thinking

Find the kth smallest value

Compare sorting the whole array, using a heap, and partition-based selection for finding the kth smallest value.

9

Sort and scan

Detect duplicate after sorting

Sort values, then scan adjacent pairs to identify duplicates and explain the runtime tradeoff.

10

Comparator design

Rank students by multiple keys

Sort records by score descending, then name ascending, then submission time ascending, and explain tie handling.

11

Two pointers

Count pairs with a target sum

Given a sorted array, count unique pairs that add to a target while skipping duplicate values correctly.

12

Two pointers

Find closest pair to a target

Given a sorted array, find the pair whose sum is closest to a target and explain how the pointers move.

13

Binary search invariant

Find minimum in rotated data

Find the smallest value in a rotated sorted array and state the invariant maintained by the search.

14

Sorting properties

Compare stable and unstable sorting

Given student records with equal grades, explain when preserving original order matters and how to test it.

15

Counting plus sorting

Sort by frequency then value

Count occurrences of values, then sort values by decreasing frequency and increasing numeric value.

16

Binary search on neighbors

Find a peak value

Find an index whose value is not smaller than its neighbors using a logarithmic search strategy.

17

Runtime analysis

Analyze nested-loop search

Given code that compares every pair of values, identify the runtime and propose a faster method using sorting or a set.

18

Empirical analysis

Benchmark three sorting inputs

Design an experiment comparing random, sorted, and reverse-sorted input for insertion sort and merge sort.

19

Binary search by index

Find missing number in sorted data

Given sorted unique values from a consecutive range with one missing value, find the missing number in logarithmic time.

20

Binary search feasibility

Split workload fairly

Given job lengths and worker count, find the smallest maximum workload that allows all jobs to be assigned in order.

21

Partition reasoning

Find median of two sorted arrays

Explain how sorted order can be used to find a median without merging every value.

22

Sorted index lookup

Count values less than each item

For each value in an array, report how many values are smaller using a sorted copy and binary search.

23

Comparator mapping

Sort strings by custom alphabet

Given a custom alphabet order, sort words by mapping each character to its rank.

24

Single scan

Find longest increasing run

Scan an array to find the longest contiguous increasing run and explain why sorting would change the problem.

25

Complexity tradeoffs

Choose an algorithm for large inputs

Given input sizes and time limits, choose between O(n), O(n log n), and O(n squared) approaches and justify the choice.

26

Trace sorting steps

Detect impossible sorted output

Given several intermediate arrays, determine which one could occur after a pass of insertion sort or selection sort.

27

Staircase search

Search a row-column sorted matrix

Find a target in a matrix where rows and columns are sorted by starting from a strategic corner.

28

Binary search over versions

Find first failing build

Given versions where all builds after the first failure also fail, find the first failing version with minimum checks.

29

Preprocessing

Optimize repeated membership checks

Given many lookup queries, decide whether sorting plus binary search or a HashSet gives the better long-term plan.

30

DSA support

Write an efficiency reflection

Create a short written analysis comparing two correct solutions by runtime, memory, readability, and input constraints.

Need support with Unit 2?

Work through the concepts, code, edge cases, and runtime analysis with 1:1 guidance.