Big-O Notation — Data Structures 练习题
Big-O notation describes how an algorithm's runtime or space grows as input size n increases. It focuses on worst-case asymptotic behavior and ignores constants and lower-order terms.
Common classes from fastest to slowest: O(1) constant, O(log n) logarithmic, O(n) linear, O(n log n) linearithmic, O(n²) quadratic, O(2ⁿ) exponential.
Examples: array index access O(1); binary search O(log n); linear scan O(n); nested loops over n O(n²); recursive Fibonacci without memo O(2ⁿ). Choose data structures to match operation needs.
练习的技能
- Classifying algorithms by Big-O
- Comparing growth rates
- Analyzing loops and nested loops
- Connecting structure choice to complexity
练习题:Big-O Notation
说明: 仔细解答每一道题。清晰地展示所有解题过程。在指定位置或按指示在另一张纸上写出最终答案。
-
1.Determine the Big-O complexity of the following function: . Provide a brief justification.
Big-O Notation — 练习题(续)
-
2.Sort the following functions in increasing order of asymptotic growth rate (from slowest to fastest): , , , , , , . Write your final ordering.
Big-O Notation — 练习题(续)
-
3.Consider the following code fragment:
for i = 1 to n: for j = 1 to i: print(i + j)What is the Big-O time complexity of this code? Explain your reasoning.
Big-O Notation — 练习题(续)
-
4.True or False: . Justify your answer.
Big-O Notation — 练习题(续)
-
5.Multiple Choice: Which of the following is equivalent to ?
- A. and
- B. only
- C. only
- D. Neither nor
Big-O Notation — 练习题(续)
-
6.Determine the Big-O complexity of the recurrence . Assume . Show your work using the Master Theorem or iteration method.
-
7.Given and , determine whether , , or . Justify your answer.
Big-O Notation — 练习题(续)
-
8.Multiple Choice: What is the Big-O complexity of the following function? \[ f(n) = \sum_{i=1}^{n} i^2 \]
- A.
- B.
- C.
- D.
Big-O Notation — 练习题(续)
-
9.Prove or disprove: . Provide a formal proof using the definition of Big-Omega.
Big-O Notation — 练习题(续)
-
10.Consider the algorithm that finds the maximum element in an unsorted array of size . What is the tightest asymptotic bound for its worst-case time complexity? Explain why.
参考答案
-
1.
The highest-order term is , so the function grows as . All lower-order terms are dominated.最终答案:
-
2.
最终答案: , , , , , ,
-
3.
The outer loop runs times, and the inner loop runs an average of times, giving approximately operations, which is .最终答案:
-
4.
, so for and all . Thus .最终答案: True
-
5.
By definition, means the function is both and .最终答案: (A) and
参考答案(续)
-
6.
Using the Master Theorem with , , , we have , and , so case 2 applies: .最终答案:
-
7.
Compare: vs . Since grows slower than , is asymptotically smaller, so but not .最终答案:
-
8.
The sum , which is .最终答案: (C)
-
9.
We need constants and such that for all . Choose and : for all . Thus .最终答案: True
-
10.
The algorithm must examine each element once to find the maximum, requiring comparisons in the worst case, which is .最终答案:
需要避免的常见错误
- Confusing best case with Big-O (usually worst case)
- Calling O(2n) different from O(n) — constants drop
- Ignoring hidden loops in string operations
- Assuming recursion is always O(2ⁿ)
生成您自己的练习表
想要一套难度自选的 big-o notation 新题?使用 WorksheetSmith 在几秒内编译一份可打印的自定义 PDF。
最后更新:2026