Big O

소바·2022년 8월 14일
0

Big O

Why it is needed?

How do we determine which code is better?
maybe it is easier to read or is code two better?
Big O is a way to mathematically figure out which of these two is better, which one runs more efficiently.

Time complexity vs Space Complexity

Time Complextity


obviously, we want our code to run as quickly as possible, be as efficient as possible. This is what is called time complexity.

But we don't measure time, complexity in time, we measure it in the number of operations, and the reason we do that is if you took the same code and we ran it on a faster computer.

Space Complexity

Space Complexity is the amount of memory that something uses.
while you know code1 is very fast and runs in 15 seconds are comparatively fast, however it may uses a lot of memory.

And then we reset all of this and we look at code2, and while that runs a really long time, full minute maybe it uses less memory.

And if this is what's most important to you, maybe code two is better.

What is more important? Time vs Space

It's up to the situation that we running code. We are going to look mostly at time complexity, but space complexity also important. So we should know about in the case of what if we care about space complexity more?

what is Big O?

kind of notations

When you're dealing with this kind of notation, there are three Greek letters that you'll consistently see.

They are Omega, Theta and Omicron. Omicron is better known as O as in Big O.

eg.

Let's say we're going to build a for loop to iterate through this array until we find a particular number.

Assume that we're looking for the number 1.

Best case

If we're looking for the number one,

this(1) is our best case with the least number of iterations through the array because if there is less repetitation we get more efficiency.

Worst case


This(7) would be our worst case

Average case


and this (4) would be our average case

Best case , Average case, Worst case


this best case is represented with the Greek letter Omega.
This average case is represented with the Greek letter Theta,
and our worst case is represented with Omicron or O

Big O is always going to be worst case. So when we measure Big O, we are always measuring worst case.

profile
소바보이

0개의 댓글