liveBook · Manning

Quadratic time complexity

As explained in Fabulous Adventures in Data Structures and Algorithms

Quadratic cost is a resource cost that grows proportionally to the square of an input's length.

Quadratic time complexity describes a cost proportional to the square of an input’s size. When the problem doubles, the cost quadruples; a tenfold increase makes it roughly one hundred times larger. section 1.1

Use the growth pattern to compare quadratic work with nearby complexity classes:

  • Linear work doubles when the input doubles, while quasilinear work grows slightly faster than doubling. section 1.1
  • Quadratic work becomes increasingly steep as input size grows, so it is usually unsuitable for large, scalable problems. section 1.1 section 1.4
  • Some implementations become quadratic accidentally when a cheaper design is available. section 1.1

Repeated string concatenation

A naïve ToString implementation that repeatedly appends to an accumulating string copies the accumulated text each time. The result is O(n²) in both time and extra memory; replacing it with StringBuilder yields an O(n) implementation. section 1.2.1

Long-context text generation

When a text generator analyzes the entire text produced so far to predict every next word, the cost of generating a document can become quadratic in its length. section 16.2

Figure 1.1 A graph comparing the growth curves for logarithmic, linear, quasilinear, quadratic, and exponential growth. The logarithmic curve is the shallowest, growing slowly, and the exponential curve is the fastest.

Compare quadratic growth visually with logarithmic, linear, quasilinear, and exponential growth. section 1.1

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Defines doubling and tenfold effects, then diagnoses accidental quadratic implementations and contrasts an O(n) replacement.
Examples
Uses naïve string accumulation and whole-history text generation as concrete quadratic-cost examples.
What it assumes
Assumes familiarity with input size and algorithmic cost while defining asymptotic scaling through concrete comparisons.
What you can do after
Diagnose accidentally quadratic code and replace repeated string concatenation with StringBuilder.
How it connects
Connects asymptotic complexity with string concatenation, StringBuilder, Markov processes, and exponential growth.

Also explained in

Compare how 4 books explain quadratic time complexity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.