Неформальный анализ времени выполнения
Введено| English | Значение |
|---|---|
| run time/rʌn taɪm/ | Время выполнения |
| linear/ˈlɪnɪə/ | Линейная сложность |
| quadratic/kwɒˈdrætɪk/ | Квадратичная сложность |
Counting the work
- We measure a program's run time 运行时间 by how many times its key statements run.
- Not seconds (those vary by machine) — a count of operations as the input grows.
- More operations for the same input size → slower algorithm.
- This lets us compare algorithms fairly, independent of hardware.
A single loop: about n
- A loop over
nitems runs its body aboutntimes. - Double the input → about double the work. This is linear 线性 growth.
- Summing an array, searching a list one-by-one: all about
nsteps. - Written informally as "order
n."

How the running time grows with the number of elements n
A nested loop: about n²
- A nested loop over
nitems runs its inner body aboutn × n = n²times. - Double the input → about four times the work. This is quadratic 二次 growth.
- Comparing all pairs, filling an
n × ngrid: aboutn²steps. n²grows much faster thannasngets large.
Comparing growth
- To compare algorithms, ask how the work grows as the input grows.
- For large
n, annalgorithm beats ann²algorithm — often by a lot. - Focus on the part of the code that runs the most (usually the innermost loop).
- The fastest-growing term dominates the total run time.
Judge run time by GROWTH, not a fixed count. An algorithm that does n steps and one that does 2n + 5 steps both grow linearly — for comparing, the constant and the +5 don't matter; the shape (n vs n²) does. A single loop is about n; a nested loop is about n², which grows far faster for large inputs.
Two ways to find duplicates in n items:
- Nested loops comparing every pair: about
n²steps. - Sort first, then one pass: far fewer steps for large
n. - For
n = 1000,n²is a million steps — then²approach is much slower.
Informal run-time analysis counts how many times statements run as the input grows. A single loop over n items is about n (linear); a nested loop is about n² (quadratic), which grows much faster. Compare algorithms by their growth, focusing on the most-executed (innermost) code.
Как растет объем работы с увеличением n
Вложенный цикл (n²) растет значительно быстрее, чем одиночный цикл (n).
Одиночный цикл по n элементам выполняет своё тело примерно сколько раз?
Сложность 1/5 · 100 монет + небольшой бонус за первое выполнение сегодня
Один цикл по n элементам → около n шагов (линейно).
Вложенный цикл по n элементам выполняет своё внутреннее тело примерно сколько раз?
Сложность 1/5 · 100 монет + небольшой бонус за первое выполнение сегодня
n × n = n² (квадратично).
Для n = 1000, примерно сколько шагов занимает алгоритм сложности n² (в миллионах)?
Сложность 2/5 · 140 монет + небольшой бонус за первое выполнение сегодня
1000² = 1,000,000 = 1 миллион.
Для больших n алгоритм за n шагов, как правило, работает быстрее, чем алгоритм за n² шагов.
Сложность 1/5 · 100 монет + небольшой бонус за первое выполнение сегодня
n² растет намного быстрее, поэтому он выполняет гораздо больше работы при больших значениях n.
Мы измеряем время выполнения в секундах, которые одинаковы на любом компьютере.
Сложность 1/5 · 100 монет + небольшой бонус за первое выполнение сегодня
Мы считаем количество операций относительно размера входных данных — время в секундах варьируется в зависимости от машины.