What do o(n) and o(log n) mean? Now that it's relevant to a current project, I might have enough context to get it.
In easier to understand terms, Big-O notation is "how many steps do I need to do something". This first example is a constant algorithm, or O(1). No matter how big the list is, it will always take 1 step to print the first index. These are the fastest possible algorithms.
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list[0])
A little slower than that is a logarithmic algorithm, or O(log n). That is when you want to do something, but it becomes expontentially
faster the more stuff you want to do. The following is a logarithmic algorithm. Every step, the index i is doubled and then printed out. Doing this for 10 numbers would take 4 steps. 100 numbers would take 7 steps. 1,000,000,000 would take 29. In most situations, O(log n) is the best you can get.
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
i = 1
while i < len(list):
print(list[i])
i = i * 2
Next there are O(n) algorithms, or linear algorithms. For every piece of data I have, I need to perform one step. The number of steps grows
linearly compared to the amount of data I have. This is a linear algorithm:
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in list:
print(i)
Even slower than that are O(n log n) algorithms. As you might've guessed, these are as slow as O(n) and O(log n) combined. Generally speaking, you have one loop that is O(n) and another that is O(log n). This algorithm looks at every index in my list, then multiplies it by index 1, 2, 4, 8, 16...
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for n in list:
i = 1
while i < len(list):
print(n * i)
i = i * 2
Lastly are quadratic O(n^2) algorithms. There are even slower algorithms than these, but they are less common. A quadratic algorithm becomes exponentially
slower the more stuff you want to do. They're like the opposite of logarithmic algorithms. In this example, I multiply every index by every other index. 10 numbers would take 100 steps. 100 numbers would take 100,000 steps. 1,000,000,000 would take 1,000,000,000,000,000,000 steps. Obviously, these are terrible algorithms.
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for n in list:
for n in list:
print(n * n)
This is an extreme simplification and I know people will chew me out for it, but as someone who just learned this stuff I know technical jargon can mask understanding.