0xCODE
Published in

0xCODE

The “Big O Notation” In Simple Terms.

Introduction To Time Complexity In Algorithms

Steps to determine the time complexity:

4 Common “O” Notations:

T = N = c * 1 = O(1)
X = ['A', 'B', 'C', 'D']
X[3] = D
print("Please select from the following items:")
print("1 - Potato Chips")
print("2 - Pretzels")
print("3 - Nachos")
print("4 - Popcorn")
optNum = input("Type number: ")
var = int(optNum)
if var == 1:
print("You have chosen Potato Chips")
print(var)
elif var == 2:
print("You have chosen Pretzels")
print(var)
elif var == 3:
print("You have chosen Nachos")
print(var)
elif var == 4:
print("You have chosen Popcorn")
print(var)
else:
print("Invalid Option, try again")
print(var)
print("Good bye!")
T = aN + B = N = O(N)
X = ['A', 'B', 'D', 'E', 'F']
X.insert(2, 'C')
X = ['A', 'B', 'C', 'D', 'E', 'F']
Y = ['apple', 'banana', 'mango', 'grapes', 'pineapple', 'durian']
def search(Y, n, x):for i in range (0, n):
if (Y[i] == x):
return i;
return -1;
Y = [‘apple’, ‘banana’, ‘mango’, ‘grapes’, ‘pineapple’, ‘durian’]x = “pineapple”;n = len(Y);
result = search(Y, n, x)
if(result == -1):
print(“Element is not present in the list”)
else:
print(“Element”, x, “is present at index”, result)
T = n*(n+1)/2 = n² + n/2 = O(N²)
X = [21, 5, 3, 18, 30, 2, 8] -> X = [2, 3, 5, 8, 18, 21, 30]
X = [21, 5, 3, 18, 30, 2, 8]def bubbleSort(X):
n = len(X)
for i in range(n):

for j in range(0, n-i-1):
if X[j] > X[j+1] :
X[j], X[j+1] = X[j+1], X[j]
bubbleSort(X)print("This is the sorted list: ")
for i in range(len(X)):
print("%d" %X[i])
X.sort()[2, 3, 5, 8, 18, 21, 30]
T = Log N² = O(Log N)
X = [10, 15, 35, 42, 60, 70, 82, 94]
def binary_search(X, item):
first = 0
last = len(X)-1
found = False
while( first<=last and not found):
mid = (first + last)//2
if X[mid] == item :
found = True
print(“The element item”, item, “was found at index “, X.index(60))
else:
if item < X[mid]:
last = mid — 1
else:
first = mid + 1
return found
print(binary_search([10, 15, 35, 42, 60, 70, 82, 94], 60))

Summary

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vincent Tabora

Editor HD-PRO, DevOps Trusterras (Cybersecurity, Blockchain, Software Development, Engineering, Photography, Technology)