Phân tích Thời gian Chạy Không chính thức
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| run time/rʌn taɪm/ | thời gian chạy |
| linear/ˈlɪnɪə/ | tuyến tính |
| quadratic/kwɒˈdrætɪk/ | bậc hai |
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.
Công việc tăng trưởng như thế nào theo n
Một vòng lặp lồng nhau (n²) tăng trưởng nhanh hơn nhiều so với một vòng lặp đơn (n).
Một vòng lặp đơn trên n mục chạy thân của nó khoảng bao nhiêu lần?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Một vòng lặp trên n mục → khoảng n bước (tuyến tính).
Một vòng lặp lồng nhau trên n mục chạy thân của nó khoảng bao nhiêu lần?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
n × n = n² (bậc hai).
Đối với n = 1000, khoảng bao nhiêu bước là một thuật toán n² (triệu)?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
1000² = 1,000,000 = 1 triệu.
Đối với n lớn, một thuật toán n bước nói chung nhanh hơn một thuật toán n² bước.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
n² tăng trưởng nhanh hơn nhiều, vì vậy nó thực hiện rất nhiều công việc hơn ở n lớn.
Chúng ta đo thời gian chạy bằng giây, điều này giống nhau trên mọi máy tính.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Chúng ta đếm số phép toán so với kích thước đầu vào — giây thay đổi tùy theo máy.