Guide to High School Computer Science
  • 💻Introduction
    • windows & Python Development
    • macOS & Python Development
    • Visual Studio Code Settings
    • Set up Github
    • Author Page
  • 🧠Prerequisite Skills
    • Keyboard Typing
    • Files & Directories
    • Use of Command Line
    • Git & GitHub
    • Markdown
    • Starting Your Python Project
  • 🐍Python Programming
    • 🍎Python Basics
      • What is Python?
      • Procedural Programming & Programming Paradigms
      • String Formatting
      • Data Types
      • Input & Output to Console
      • Working with Numbers
      • Useful Built-in Functions
      • Math & Random Module
      • Boolean Data Object
      • Comparison, Logical, and Membership Operators
      • If Statements
      • Binary Decisions
      • Multiple Decisions
      • Nested Conditions
      • [EXTRA] Bitwise Operators
      • [EXTRA] Python Style Guide
    • ⏮️Iterations
      • Introduction to While Loops
      • Infinite Loop
      • Controlling Your While Loops
      • Introduction to For Loops
      • For Loops w/ Numeric Sequences
      • For Loops w/ Strings & Lists
      • Iterable Functions w/ For Loops
    • 📦Collections
      • Strings
        • String Basics
        • String Indexing
        • String Slicing
        • String Operators & Functions
        • Basic String Methods
        • String Methods Extended
        • String Methods Document
      • Tuples & Lists
        • Tuples
        • List Basics
        • List are Mutable
        • Adding Items to a List
        • Removing Items from a List
        • Search & Reverse a List
        • List Comprehension
        • List Methods Document
      • Sets
      • Dictionary
      • How to Store Multiple Data Items
    • 💡Defining Functions
      • Functions
      • print() vs return
      • Pre-determined Arguments
      • Nested Functions
      • Map & Filter
      • [Extra] Dynamic Arguments
    • 💾File I/O
      • How to Save Text to an External File
      • Reading CSV in Python
      • Reading JSON in Python
    • 🔨Basic Python Projects
      • Basic Calculator
        • Improving the calculator
        • Exercise Set 1
        • Exercise Set 2
        • 💎Streamlit Application #1
      • Basic Password Generator
        • Exercise Set 3
        • Exercises Related to Math
        • 💎Streamlit Application #2
      • A To-Do Task List
    • ⏳Introduction to Algorithmic Thinking
      • Big-O Notation
      • Basic Algorithms
        • Linear Search
        • Binary Search
        • Basic Sorting Algorithms
      • Recursion
      • Brute Force Algorithms
      • Greedy Algorithm
        • Time on Task (CCC 2013 J4)
        • Dijkstra’s Algorithm
      • Divide and Conquer
        • Merge Sort
      • Dynamic Programming
    • 🤯Object Oriented Programming
      • Class & Objects (Definitions)
      • OOP in Python
      • Encapsulation
      • Polymorphism
      • Inheritance & Overriding
      • Override Magic Methods
      • Case Study: 2D Vectors
      • Case Study: Deck of Cards
      • Exercise
      • Abstract Data Types
      • Case Study: Static 1D Array From Java
    • Competitive Programming
      • Is This Sum Possible?
        • Is the dataset sorted?
        • Searching for a value
        • Determine if the difference between an integer from the array and the target value exists
        • Sorting Algorithms
        • Using Two Pointers
      • Two Sum - LeetCode
        • Generate all possible pairs of values
        • Subtract each value from the target, see if the difference exists in the list
      • Longest Common Prefix - LeetCode
        • Compare all possible prefixes
        • Create the longest common prefix with the direct neighbour
      • Length of Last Word - LeetCode
        • Compare all possible prefixes
      • Where can I go from one point to another?
      • Sample Outline
    • IB Recipe Book
  • 💾Python & Databases
    • Intro to Databases & Data Modeling
      • Common Data Types in SQL
      • Introduction to ERDs
      • Primary Keys and Foreign Keys
      • Database Normalization
    • What is SQL?
      • Getting Started
      • SELECT Queries
        • Selection with Conditions
        • Selection with Fuzziness
        • Selection and Sorting in Order
        • Selection without Duplicates
        • Selection with Limited Number of Outputs
      • AGGREGATE Queries
        • Counting Rows
        • Sum, Average, Min/Max Queries
        • Working with Aggregate Queries
        • Power of using Groups
        • Exercise
      • Interacting with Multiple Table
      • Inserting Data
      • External Resource
  • ☕Java Essentials
    • Basics
      • Starting Java
      • Data & Variables
      • Handling User Inputs & Type Conversion
      • Arithmetic
      • IPO Model
      • Basic Built-in Methods
      • Exercise Questions
    • Conditionals
      • Boolean Operators
      • Compare Strings
      • If Statements
      • If Else Statements
      • Making Multiple Decisions
      • Using Switch
      • Flowchart Symbols
      • Exercise Questions
    • Iterations
      • While Loops
      • For Loop
      • Exercises
    • Java Type Casting
    • Strings
      • Common String Practices
      • String Formatting
      • Java Special Characters
    • Collection
      • Arrays
      • For Each Loop
      • ArrayList
      • Exercise Questions
    • Static Methods
      • (Aside) Clearing your Console
    • Randomness in Java
    • Delayed Output in Java
    • Java Output Formatting
    • Java Style Guide
  • 🛠️JavaScript Programming
    • Our Programming Editor & Workflow
      • Hello, world!
      • Commenting & Variables
      • Data in JavaScript
      • Operators
      • String Formatting
      • Getting User Input
    • JavaScript Exercise Set 1
    • Making Decisions
      • Comparing Values
      • Combining Boolean Comparisons
      • Creating Branches
    • JavaScript Exercise Set 2
    • While Loops
      • Infinite While Loop
      • While Loops and Numbers
      • While Loops and Flags
      • While loops w/ Strings
    • JavaScript Exercise Set 3
    • Subprograms & Functions
      • Creating a Function in JavaScript
      • Function with Input and Assignable Output
    • JavaScript Exercise Set 4
  • 💾Topics in CS
    • Computer Environments & Systems
      • Computer Components
        • In-depth Explanations
      • File Maintenance
      • Computer & Safety
      • Software Development
      • Bits & Binary
    • Careers related to Computer Science
    • Postsecondary Opportunities
Powered by GitBook
On this page
  • Algorithm Complexity of Time & Space
  • Big - O Notation
  • Python 3 Data Structure and Big-O Notations
  • Example Big O Classification
  1. Python Programming
  2. Introduction to Algorithmic Thinking

Big-O Notation

PreviousIntroduction to Algorithmic ThinkingNextBasic Algorithms

Last updated 6 months ago

Algorithm Complexity of Time & Space

As computer scientists started to develop various algorithms for a solutions, they needed a way to classify the effectiveness of their algorithms, and they also required a way to prove that a new algorithm is better than the old by mathematical proof & analysis.

Due to this, three notations methods were developed:

  1. Big - O Notation (the worst case scenario)

  2. Big - Theta Notation (the average case scenario)

  3. Big- Omega Notation (the best case scenario)

When we analyze an algorithm we can look at all three scenarios and see their effectiveness. The main notation that we will focus will be the Big-O Notation. We focus on the Big-O because it shows us that at worst our algorithm will perform at X, and if X is better than our previous algorithm, it is definitely better.

Big - O Notation

Big O Notation: a mathematical notation that describes the limiting behaviour of a function as its input/parameter/argument approaches infinity.

  • Consider the Big-O to tell us the “Worst case scenario performance” of our algorithm

We use Big-O Notation for:

  1. Algorithm Proof: To prove that our algorithm A is better than algorithm B, we must have a proof that our Big-O notation is better

  2. Measure Performance, Run-time, or Disk Usage: Our algorithms are designed to solve problems; however, reaching the solutions must not be feasible due to time or disk-space constraints

  3. Mathematically Formalizing our Algorithms: Different hardware will output different runtimes; therefore, we needed a formal mathematical analysis

Big-O is Classified with a Notation of:

O(f(x))

-- where f(x) can be various mathematical functions that describes behaviour of our algorithm's effectiveness.

The following is a common list of complexities from best to worst performance:

- O(1) → Constant Complexity
- O(log n) → Logarithmic Complexity
- O(n) → Linear Complexity
- O(n log n) → Linearithmic Complexity
- O(n^2) → Quadratic Complexity
- O(2^n)→ Exponential Complexity
- O(n!)→ Factorial Complexity

Time & Space Complexity

  • Time: the measure of how long an algorithm takes

  • Space: the measure of how much disk space that the algorithm requires from the computer

Python 3 Data Structure and Big-O Notations

Lists and Tuples:

Operation
Example
Class
Notes

Index

l[i]

O(1)

Store

l[i] = 0

O(1)

Length

len(l)

O(1)

Append

l.append(5)

O(1)

Pop

l.pop()

O(1)

same as l.pop(-1), popping at end

Clear

l.clear()

O(1)

similar to l = []

Slice

l[a:b]

O(b-a)

l[1:5]:O(l)/l[:]:O(len(l)-0)=O(N)

Extend

l.extend(…)

O(len(…))

depends only on len of extension

Construction

list(…)

O(len(…))

depends on length of argument

—–

—–

—–

—–

check ==, !=

l1 == l2

O(N)

Insert

l[a:b] = …

O(N)

Delete

del l[i]

O(N)

Remove

l.remove(…)

O(N)

Containment

x in / not in l

O(N)

searches list

Copy

l.copy()

O(N)

Same as l[:] which is O(N)

Pop

l.pop(0)

O(N)

Extreme value

min(l)/max(l)

O(N)

Reverse

l.reverse()

O(N)

Iteration

for v in l:

O(N)

—–

—–

—–

—–

Sort

l.sort()

O(N Log N)

key/reverse doesn’t change this

Multiply

k*l

O(k N)

5*l is O(N): len(l)*l is O(N**2)

Tuples support all operations that do not mutate the data structure (and with the same complexity classes).

Sets:

Operation
Example
Class
Notes

Length

len(s)

O(1)

Add

s.add(5)

O(1)

Containment

x in/not in s

O(1)

compare to list/tuple - O(N)

Remove

s.remove(5)

O(1)

compare to list/tuple - O(N)

Discard

s.discard(5)

O(1)

Pop

s.pop()

O(1)

compare to list - O(N)

Clear

s.clear()

O(1)

similar to s = set()

—–

—–

—–

—–

Construction

set(…)

len(…)

check ==, !=

s != t

O(min(len(s),lent(t))

<=/<

s <= t

O(len(s1))

issubset

>=/>

s >= t

O(len(s2))

issuperset s <= t == t >= s

Union

s

t

O(len(s)+len(t))

Intersection

s & t

O(min(len(s),lent(t))

Difference

s - t

O(len(t))

Symmetric Difference

s ^ t

O(len(s))

—–

—–

—–

—–

Iteration

for v in s:

O(N)

Copy

s.copy()

O(N)

Dictionary:

Operation
Example
Class
Notes

Index

d[k]

O(1)

Store

d[k] = v

O(1)

Length

len(d)

O(1)

Delete

del d[k]

O(1)

get/setdefault

d.method

O(1)

Pop

d.pop(k)

O(1)

Pop item

d.popitem()

O(1)

Clear

d.clear()

O(1)

similar to s = {} or = dict()

Views

d.keys()

O(1)

—–

—–

—–

—–

Construction

dict(…)

len(…)

—–

—–

—–

—–

Iteration

for k in d:

O(N)

all forms: keys, values, items

Example Big O Classification

def factor_counter(n):
    """ factor_counter() determines the number of factors N has """

    ctr = 0
    for divisor in range(1,n+1):
        if n % divisor == 0:
            ctr += 1

    return ctr
# end of factor_counter

We look at every value of N from 1 to N; therefore, this is O(n).

  • Minor computations such as: += 1 and n % divisor does not affect the overall classification

def factor_counter(n):
    """ factor_counter() determines the number of factors N has """

    ctr = 0
    if n < 9:
        # difference of speed between the optimized and non-optimized is very minimial on small numbers
        for divisor in range(1,n+1):
            if n % divisor == 0:
                ctr += 1

        return ctr
    else:
        end_point = int(n**0.5) + 1

        for divisor in range(1, end_point):
            if n % divisor == 0 and (n // divisor) != divisor:
                ctr += 2
            elif n % divisor == 0 and (n // divisor) == divisor:
                ctr += 1

    return ctr

The refactored version only analyzes value from 1 to SquareRoot(n); therefore, the refactored version is:

O(n)=nO(n) = \sqrt{n}O(n)=n​

🐍
⏳
Source
Image Source