CompSci Notes online part 1.

Overview of Complexity
  • Focuses on Time Complexity (execution time) rather than code logic.

  • Storage Complexity (memory usage) is a secondary consideration.

  • Execution time depends on hardware; thus, efficiency is measured by the number of statement executions relative to input size nn.

Comparing Algorithms
  • Method 1 (Nested Loops): Iterates through rows and columns (ii and jj). Total executions amount to I×JI \times J, or O(n2)O(n^2) for a square matrix.

  • Method 2 (Single Loop): Iterates through the diagonal once (i=ji = j). Total executions amount to II, or O(n)O(n).

Big O Notation Principles
  • Definition: Predicts growth rate of runtime as input size nn increases.

  • Rules for Calculation:

    • Focus on the highest order term (e.g., n2n^2 dominates nn).

    • Ignore constants (e.g., 3n23n^2 becomes O(n2)O(n^2); n/2n/2 becomes O(n)O(n)).

    • Multiple variables must be preserved (e.g., 4m+3n4m + 3n becomes O(n+m)O(n + m)).

Common Big O Classes
  • O(1)O(1) (Constant): Runtime is independent of input size.

  • O(logn)O(\log n) (Logarithmic): Runtime increases slowly.

  • O(n)O(n) (Linear): Runtime increases proportionally with input.

  • O(n2)O(n^2) (Quadratic): Runtime increases dramatically as nn grows.

Practical Application
  • Analyzing real code allows developers to choose the most efficient algorithm based on predicted scaling behavior rather than just correctness.