大 O:O(1)、O(n)、O(n²)
大 O 說的是步數隨 n 漲得多快:去掉常數和小項,只留漲得最快的一項,所以 3n + 2 是 O(n),兩層迴圈是 O(n²),v[i] 是 O(1)。
上一課數出了步數:找最大值大約 n 步,兩層迴圈大約 n² 步。大 O 就是把這種說法寫短:O(n)、O(n²)。它不管具體是 3n + 2 還是 5n,只管漲法。
大 O 說的是什麼
寫大 O 只做兩件事:
- 去掉係數:
3n和n一樣都是 O(n)。係數只差幾倍,漲法相同。 - 只留最大的那項:
n² + 100n + 7是 O(n²)。n 一大,後面的都不值一提。
所以 3n + 2 是 O(n),n²/2 + n/2 是 O(n²),20 是 O(1)。
3n² + 5n + 100 寫成大 O 是什麼?
O(1):和 n 沒關係
按下標取 vector 的一個元素,不管它多長,都是一步算出位址(開頭的位址 + i × 每個元素的大小)、一步取出來。
1#include <iostream>2#include <vector>3using namespace std;4 5int main() {6 vector<int> a(100000);7 a[99999] = 42;8 // 不管 a 多長,取 a[i] 都是這一步9 cout << a[99999] << "\n";10 cout << a.size() << "\n"; // size() 也是 O(1):vector 自己記著長度11 return 0;12}
42 100000
交換兩個變數、算一個式子、return,都是 O(1):步數是個固定的數。
in 是陣列的長度。哪段程式碼是 O(1)?
O(n):走一遍
在數組裡找一個數,最壞要從頭看到尾。
i1for (int i = 0; i < n; i++)2 if (a[i] == x)3 return i;4return -1;
一層迴圈、每次做的事是 O(1),整體就是 O(n)。
一個 O(n) 的程式,n = 1000 時要 1 毫秒。n = 4000 時大約要多久?
O(n²):兩層迴圈
問「數組裡有沒有重複的數」,最直接的寫法是兩兩比一遍:
1#include <iostream>2#include <vector>3using namespace std;4 5bool has_dup(const vector<int>& a) {6 int n = a.size();7 for (int i = 0; i < n; i++)8 for (int j = i + 1; j < n; j++)9 if (a[i] == a[j])10 return true;11 return false;12}13 14int main() {15 vector<int> a = {3, 8, 1, 9, 8};16 cout << boolalpha << has_dup(a) << "\n";17 return 0;18}
true
(boolalpha 讓 cout 把 bool 打成 true / false,不然是 1 / 0。)
裡面那層只走到 i,n = 6 時印出幾?
1#include <iostream>2using namespace std;3 4int main() {5 int n = 6, steps = 0;6 for (int i = 0; i < n; i++)7 for (int j = 0; j < i; j++)8 steps++;9 cout << steps << "\n";10 return 0;11}
一個 O(n²) 的程式,n = 1000 時要 1 秒。n 變成 2000,大約要多久?
有沒有重複
✓補全 has_dup:用兩層迴圈兩兩比較,有重複的數就返回 true,沒有就返回 false。讓程式印出下面兩行。
true false
1#include <iostream>2#include <vector>3using namespace std;4 5bool has_dup(const vector<int>& a) {6 int n = a.size();7 // 兩兩比一遍8 return false;9}10 11int main() {12 vector<int> a = {3, 8, 1, 9, 8};13 vector<int> b = {3, 8, 1, 9, 7};14 cout << boolalpha << has_dup(a) << "\n" << has_dup(b) << "\n";15 return 0;16}
看程式碼,定大 O
不用每次都數,記住三條:
| 程式碼長這樣 | 大 O | 為什麼 |
|---|---|---|
| 一段接一段(先一層迴圈,再一層迴圈) | 取最大的那段 | O(n) + O(n) 還是 O(n);O(n) + O(n²) 是 O(n²) |
| 迴圈套迴圈 | 相乘 | 外層 n 次 × 裡層 n 次 = O(n²) |
| 每次把規模砍一半 | O(log n) | 下一課講 |
考試怎麼做: 「求時間複雜度」的題,找最裡層那條語句執行了幾次,寫成 n 的式子,再去掉係數和小項。
int cnt = 0;①for (int i = 0; i < n; i++)② for (int j = 0; j < n; j++)③ if (a[i] == a[j]) cnt++;④for (int k = 0; k < n; k++)⑤ sum += a[k];
- ①迴圈外只做一次:O(1),可以不管
- ②外層:i 從 0 到 n − 1,走 n 次
- ③外層每走一次,裡層走 n 次
- ④最裡層的語句:n × n = n² 次
- ⑤後面接著的一段:n 次,和兩層迴圈那段相加
這段程式碼是 O(幾)?
1for (int i = 0; i < n; i++)2 for (int j = 0; j < n; j++)3 for (int k = 0; k < n; k++)4 cnt++;
只有一層迴圈。它是 O(幾)?
1for (int i = 0; i < n * n; i++)2 cnt++;
cnt++ 執行 n² 次,所以是 O(n²)。一段接一段
✓先一層迴圈走 n 次,再兩層迴圈走 n × n 次。補全計數,印出 n = 10 時的總步數。
110
1#include <iostream>2using namespace std;3 4int main() {5 int n = 10, steps = 0;6 for (int i = 0; i < n; i++)7 steps++;8 // 再來兩層迴圈,每層 n 次,每次 steps 加 19 cout << steps << "\n";10 return 0;11}
最好、最壞和平均
同一段程式碼,資料不一樣,步數也可能不一樣。拿順序查詢來說:要找的數正好是第一個,看 1 次就完;在最後一個或者根本不在,就要把 n 個都看完。
search(a, 4)看 1 次第一個就是search(a, 6)看 5 次全看完才知道沒有1#include <iostream>2#include <vector>3using namespace std;4 5int looks = 0;6 7int search(const vector<int>& a, int x) {8 for (int i = 0; i < (int)a.size(); i++) {9 looks++;10 if (a[i] == x) return i;11 }12 return -1;13}14 15int main() {16 vector<int> a = {4, 9, 2, 5, 7};17 for (int x : {4, 2, 6}) {18 looks = 0;19 int at = search(a, x);20 cout << "找 " << x << ":下標 " << at << ",看了 " << looks << " 次\n";21 }22 return 0;23}
找 4:下標 0,看了 1 次 找 2:下標 2,看了 3 次 找 6:下標 -1,看了 5 次
- 最好情況:步數最少的那種資料。順序查詢是 1 次,O(1)。
- 最壞情況:步數最多的那種資料。順序查詢是 n 次,O(n)。
- 平均情況:要找的數在每個位置的可能一樣大時,平均看 (1 + 2 + … + n) / n = (n + 1) / 2 次,還是 O(n)。
平常說「順序查詢是 O(n)」,說的是最壞情況:再倒霉也不會比它更慢。題目沒說是哪種時,就按最壞情況答。
用「每一對比一次」查有沒有重複,一找到就停。這組數比較了幾次?
1#include <iostream>2using namespace std;3 4int main() {5 int a[] = {5, 5, 1, 9, 3};6 int n = 5, cmp = 0;7 bool found = false;8 for (int i = 0; i < n && !found; i++)9 for (int j = i + 1; j < n && !found; j++) {10 cmp++;11 if (a[i] == a[j]) found = true;12 }13 cout << cmp << "\n";14 return 0;15}
在 n 個數裡順序查詢,要找的數一定在數組裡,在每個位置的可能一樣大。平均要比較幾次?
用 STL 寫
數「最裡層那條語句」時,要把 STL 裡藏著的迴圈也算進去。find(b, e, x) 從頭比到找到為止,count(b, e, x) 每個都比:都是 O(n)。它們的「帶條件」版本 find_if、count_if 接受一個 lambda,正好用來數比了幾次。
下面這段「有沒有重複」只有一層迴圈,可每一圈都調一次 count_if:
1#include <iostream>2#include <vector>3#include <algorithm>4using namespace std;5 6int main() {7 vector<int> a = {3, 8, 1, 9, 7};8 int steps = 0;9 bool dup = false;10 for (int x : a) {11 int c = count_if(a.begin(), a.end(), [&](int y) {12 steps++; // count_if 裡面每看一個,數一次13 return y == x;14 });15 if (c > 1) dup = true;16 }17 cout << boolalpha << dup << "\n";18 cout << "比較了 " << steps << " 次\n";19 return 0;20}
false 比較了 25 次
5 個數比了 25 次:外面一層 n 次,count_if 裡面又是 n 次,n × n = O(n²),和兩層 for 一樣。程式碼短了,複雜度一點沒少。
| 寫法 | 代價 | 為什麼 |
|---|---|---|
a[i]、a.size()、a.back() | O(1) | 算出位址、讀出記著的長度 |
find(b, e, x)、find_if | O(n) | 從頭比,最壞比完 |
count(b, e, x)、count_if | O(n) | 每個都比 |
迴圈裡調 find / count | O(n²) | n 次 × 每次 n |
面試裡: 「這段程式碼的複雜度是多少」常常就考這個:迴圈裡藏了一個 find、count 或 erase,複雜度就從 O(n) 變成 O(n²)。答的時候點出那一行,再說怎麼改(比如先排序,或者換成 unordered_set,後面幾章講)。
一層 for 裡調 count_if,lambda 數比較次數。印出什麼?
1#include <iostream>2#include <vector>3#include <algorithm>4using namespace std;5 6int main() {7 vector<int> a = {2, 7, 2, 5};8 int steps = 0, dup = 0;9 for (int x : a) {10 int c = count_if(a.begin(), a.end(), [&](int y) {11 steps++;12 return y == x;13 });14 if (c > 1) dup++;15 }16 cout << dup << " " << steps << "\n";17 return 0;18}
count_if 都把 4 個看一遍:4 × 4 = 16 次,O(n²)。2 出現了兩次,外層碰到它兩回,dup 加了 2 次。小結
大 O:去掉係數,只留漲得最快的那一項
- O(1):步數固定;O(n):走一遍;O(n²):兩層迴圈
- 一段接一段取最大,迴圈套迴圈相乘
- 考試:數最裡層語句執行幾次
- 迴圈裡的 find、count 也是一層迴圈
- 最好、最壞、平均可以不一樣;沒說明時,按最壞情況
1a[i]; // O(1)2for (int i = 0; i < n; i++) ... // O(n)3for (...) for (...) ... // O(n²)4for (int x : a) count(b, e, x); // 也是 O(n²)