Computational complexity

by Bhoomika Agarwal

Types:
1. Time
2. Space

 

Big O notation

worst-case complexity

a.k.a 'oh no!' complexity

O(1)

  • Constant runtime
  • Independent of input size
items = [0, 1, 2, 3, 4, 5]

print(item[0]) # O(1)
print(item[1]) # O(1)

O(n)

  • Linear time
items = [0, 1, 2, 3, 4, 5]

for item in items:  # O(n)
	print(item)  # O(1)

O(n²)

  • Quadratic time
items = [0, 1, 2, 3, 4, 5]

for x in range(len(items)):  # O(n)
	for y in range(len(items)): # O(n)
      print(items[x], items[y])

More complexity classes

Image Source: images (241×209) (gstatic.com)

Try it out yourself:
https://www.bigocheatsheet.com

https://visualgo.net/en/sorting

Thank you!

Computational complexity

By Bhoomika Agarwal

Computational complexity

  • 259