01 Big-O of An Algorithm

Updated 4 Oct 2026

  • Big-O used to measure the speed of an algorithm

Efficiency of an Algorithm

  • The algorithm is a set of processes for a task
  • Efficiency tells how good or bad the algorithm is, can be measured in terms of time and space.

The less time (how fast) and less space (how much memory), the better the operation (algorithm)

Counting Primitive Operations

  • Simplest analytical way to measure runtime is to count the number of primitive operations
  • By assuming. that each primitive operation has constant execution time

The more operations, the more running time

7 Primitive Operations

  1. Assigning a value x = 5
  2. Calling a method Max(5,4)
  3. Performing an arithmetic operations (+, -, , /, %) x+5
  4. Comparing two numbers x>y
  5. Indexing into an array A[1]
  6. Following an object reference c.radius
  7. Returning from a method return x;

Complexity Function

  • Can be used to describe the number of basic operations of data structure of size nn
  • It’s normally denoted as f(n)f(n)
  • f(n)f(n) tells numbers of steps and also time

The more f(n)f(n), the more running time. The less f(n)f(n), the less running time.

Big-O Notation

  • Big O notation is a mathematical notation that describes how an algorithm's time or space requirements grow as the size of its input increases. (Maybe to ∞\infty)
f(n)≤c(g(n))f(n) \le c(g(n))
  • That is f(n)f(n) is less than or equal to another function g(n)g(n) up to a constant factor and in the asymptotic sense as nn grows toward ∞\infty
  • Big-O denotes as O(…)O(…) (inside is a function of nn with no coefficient)
    • It approximates the performance for a very large nn
    • Given the input size nn, an algorithm analysis focuses only on growth rate of time or space used
      • Ex. O(n)O(n), O(n2)O(n^2), O(log⁡n)O(\log n)
    • Big-O implies how much space (how much memory) and time (how fast) is needed approximately to run an algorithm
  • The highest degree term controls the asymptotic growth rate of f(n)f(n)
  • Big-O can be used to compare the algorithms’ performances
    • เวลา Compare ก็ให้มองเป็นโจทย์แบบเปรียบเทียบ

Counting Terms in Arithmetic Sequences

  • อันนี้เอาไว้ใช้หาจำนวน Term ง่าย ๆ แบบเป็นสูตรเฉย ๆ
an=a1+(n−1)da_n=a_1+(n-1)d
  • โดยหา nn คือจำนวน Term จากสมการนี้ – เดี๋ยวได้ใช้ ใน For Loop เพื่อนับจำนวนครั้ง
  • อนุกรมเลขคณิต \ceSn=n2(a1+an)\boxed{\ce{S_n}=\frac{n}{2}(a_1+a_n)}