COURSE: Computer Science / Software Engineering 201
SUBJECT: Advanced Data Structures, Big-O Notation, and Dynamic
Programming
DOCUMENT TYPE: Advanced Lecture Notes & Algorithm Analysis
AUTHOR: Carlwyn Melgarejo
CHAPTER 1: ASYMPTOTIC ANALYSIS & BIG-O NOTATION
Asymptotic analysis defines the mathematical boundaries of an algorithm's
performance in terms of execution time and memory consumption as the
input size grows.
1. Time Complexity Classes
O(1) - Constant Time: The execution time remains independent of
the input size. (e.g., Array index lookup).
\(O(\log n)\) - Logarithmic Time: The input size is halved at each
operational step. (e.g., Binary Search).
O(n) - Linear Time: Execution time scales proportionally with input
size n. (e.g., Linear Search loop).
\(O(n \log n)\) - Linearithmic Time: Typical of efficient sorting
algorithms. (e.g., Merge Sort, Quick Sort).
O(n²) - Quadratic Time: Performance is proportional to the square of
the input size. (e.g., Nested loops / Bubble Sort).
2. Mathematical Code Analysis Example
Consider the following nested loop structure tracking matrix operations:
python
def analyze_matrix(matrix, n):
count = 0
# Outer loop executes n times
for i in range(n):
# Inner loop executes n times
for j in range(n):
count += matrix[i][j] # O(1) operation
return count
Use code with caution.
Complexity Evaluation: Because the algorithm contains two nested loops
iterating up to n, the total execution block runs n × n times. Hence, the
worst-case time complexity is explicitly O(n²).