Skip to the content.

Big O and Algorithm Efficiency Hacks

Homework Hacks

Create examples for each of the following time complexities:

  • O(1)
  • O(log n)
  • O(n)
  • O(n²)

O(1): Constant Time

def print_second_name(arr):
    return arr[1]

O(log n): Logarithmic Time

def power(a, b):
    result = 1
    while b > 0:
        if b % 2 == 1:
            result *= a
        a *= a
        b //= 2
    return result

O(n): Linear Time

for color in colorlist:
    print(color)

O(n²): Polynomial Time

for x in data:
    for y in data:
        print(x, y)