Jane Doe
Pro Plan
The following are the most common cases of Big O a programmer should be aware of.
They're sorted from best to worse case.
No matter how much data there is, it only takes one operation.
def get_first(nums): return nums[0] get_first([1, 2, 3, 4, 5, 6])
Every step/action you take, you cut out half of the work.
def binary_search(nums, target):
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
binary_search([1, 2, 3, 4, 5, 6], 4)Complexity grows in direct proportion to the size of the input/s.
def print_items(nums): for num in nums: print(num) print_items([1, 2, 3, 4, 5, 6])
Often is a nested iteration where within each operation done in linear time, there are actions being done in logarithmic time over the same size of data.
Nested loops over the same or similarly sized dataset.
Triple nested loops over the same or similarly sized dataset. The number of operations grows with the cube of the input size.
The number of operations doubles with each additional input. Often seen in recursive problems that branch into two or more calls per step.
The number of operations doubles with each additional input. Often seen in recursive problems that branch into two or more calls per step.
Checkout this cheatsheet for more details.