Evanalysis
2.1Source-backedEmbedded interactionEstimated reading time: 2 min

2.1 Complexity growth and algorithmic cost

Compare O(1), O(log n), O(n), O(n log n), and O(n^2) growth rigorously before coding.

Note collections

CSCI2520: Data structures

Structured notes for CSCI2520 data-structure foundations with operation-level reasoning and selective interactive demonstrations.

Chapter 1

ADT and operation semantics

From ADT contracts to operation behavior in stack/queue implementations.

Chapter 2

Complexity and sorting

Asymptotic growth, cost comparison, and sorting-oriented complexity reasoning.

Why complexity first

Before implementation details, we need a growth model. Asymptotic analysis captures how running cost scales with input size n, which is often more important than constant-factor micro-optimizations.

Cost model and Big-O

Definition

Asymptotic upper bound (Big-O)

T(n)=O(f(n))T(n)=O(f(n)) means there exist constants C>0C>0 and n0n_0 such that T(n)Cf(n)T(n)\le C f(n) for all nn0n\ge n_0.

This definition is a proof obligation: you must show constants and a valid threshold, not only say “it looks quadratic”.

Typical growth classes in CSCI2520

  • O(1): direct index access, pointer re-link (with known node addresses).
  • O(log n): balanced-tree search, binary-search style halving.
  • O(n): full scan in array/list.
  • O(n log n): merge/heap style divide + linear merge phase.
  • O(n2)O(n^2): nested scan (e.g. simple comparison-based double loops).

Read and try

Compare asymptotic growth at one n

Compares growth classes at the same n so relative scaling becomes concrete.

ClassValue at n=16
O(1)1.00
O(log n)4.00
O(n)16.00
O(n log n)64.00
O(n^2)256.00

Worked comparison

Worked example

When does n log n dominate n?

For n=8n=8, nlog2n=24n log_2 n = 24 and n=8n = 8, so n log n is already larger. As n grows, n log n stays asymptotically below n2n^2 but above linear.

Quick checks

Quick check

If an algorithm has two nested loops each running n times, what class is the total cost?

Assume loop body is O(1).

Solution

Answer

Quick check

Why does O(n log n) beat O(n^2) for sufficiently large n?

Compare ratio (n^2)/(n log n).

Solution

Answer