Introduction to Algorithms

Introduction to Algorithms

Understanding the basics of algorithmic complexity and Big O notation.

What is an Algorithm?

An algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to get the desired output.

Characteristics

  1. Unambiguous: Algorithm should be clear and unambiguous.
  2. Input: An algorithm should have 0 or more well-defined inputs.
  3. Output: An algorithm should have 1 or more well-defined outputs.

Big O Notation

Big O notation is used to classify algorithms according to how their run time or space requirements grow as the input size grows.

# Example: O(n) Complexity
def find_max(arr):
    max_val = arr[0]
    for num in arr:
        if num > max_val:
            max_val = num
    return max_val

Common Complexities

  • O(1): Constant Time
  • O(log n): Logarithmic Time
  • O(n): Linear Time
  • O(n^2): Quadratic Time

See Binary Search for an example of O(log n).

Sponsored
Mobile Ad Banner