Big-O Complexity Chart

소바·2022년 8월 14일
0

O(n)

and I start with O of n not because it's the most efficient or least efficient, but because I think it's the one that's easiest to get your head.

And I'm going to call this function log items and we're just going to console log out some items.

so let's go look at this in chrome dev tools.

And you can see that we've console logged out zero through nine.

So to put it another way, we pass the function, the number n and this rans n times, so we pass it 10 and it ran 10 times and output 10 things. That's what O of n is.

O of n is always going to be a straight line, it is proportional, the number of operations is going to be proportional to whatever n is.

data stuructue : Array

That means that if you search by value.

This is one of the big advantages of a raise, is that we can find something in an array with a million items in it and go to item 400000, say, and it is an old one.

But those indexes are also a disadvantage for the array's if you're adding something to the beginning, as we just saw, because you're going to have to index everything.

So the big thing that you have to look at when you're looking at a data structure is what are you using this for.

If you need to access things by index arrays are a great data structure.

But if you search by index and you say, tell me what's at the index of three, those indexes allow you to go directly to that place in memory as an 0(1) operation.

But if you're going to be adding and removing a lot of items from the beginning, maybe not the best data structure for you

Big-O Cheat Sheet

profile
소바보이

0개의 댓글