## How does Big-O Notation work, and can you provide an example? |
||||

First and foremost, do not even walk into a software interview without knowing what Big O Analysis is all about – you will embarrass yourself. Big O Notation is simply something that you must know if you expect to get a job in this industry. Here we present a tutorial on Big O Notation, along with some simple examples to really help you understand it. You can consider this article to be sort of a big O notation for dummies tutorial, because we really try to make it easy to understand. ## What is Big O Analysis in computer science – a tutorial:
When solving a computer science problem there will usually be more than just one solution. These solutions will often be in the form of different algorithms, and you will generally want to compare the algorithms to see which one is more efficient. This is where Big O analysis helps – it gives us some basis for measuring the efficiency of an algorithm. A more detailed explanation and definition of Big O analysis would be this: it measures the efficiency of an algorithm based on the Sounds quite boring, right? It’s really not that bad at all – and it is something best illustrated by an example with actual code samples. ## Big O Notation Practice ProblemsEven if you already know what Big O Notation is, you can still check out the example algorithms below and try to figure out the Big O Notation of each algorithm on your own without reading our answers first. This will give you some good practice finding the Big O Notation on your own using the problems below. ## Big O Notation Examples in Java
Now it’s really time to pay attention – let’s start our explanation of Big O Notation with an actual problem. Here is the problem we are trying to solve:
In order to best illustrate the way Big-O analysis works, we will come up with Here’s our first function that will simply return the integer that is the smallest in the array. The algorithm will just iterate through all of the values in the array and keep track of the smallest integer in the array in the variable called curMin. Let’s assume that the array being passed to our function contains 10 elements – this number is something we arbitrarily chose. We could have said it contains 100, or 100000 elements – either way it would have made no difference for our purposes here. ## The CompareSmallestNumber Java function
As promised, we want to show you another solution to the problem. In this solution, we will use a different algorithm - we will soon compare the big O Notation of the two different solutions below. What we do for our second solution to the problem is compare each value in the array to all of the other numbers in the array, and if that value is less than or equal to all of the other numbers in the array then we know that it is the smallest number in the array.
Now, you've seen 2 functions that solve the same problem - but each one uses a different algorithm. We want to be able to say which algorithm is more efficient using mathematical terms, and Big-O analysis allows us to do exactly that. ## Big O analysis of algorithmsFor our purposes, we assumed an input size of 10 for the array. But when doing Big O analysis, we don't want to use specific numbers for the input size - so we say that the input is of size n. Remember that Big-O analysis is used to measure the efficiency of an algorithm based on the When doing Big-O analysis, "input" can mean a lot of different things depending on the problem being solved. In our examples above, the input is the array that is passed into the different functions. But, input could also be the number of elements of a linked list, the nodes in a tree, or whatever data structure you are dealing with. Since input is of size n, and in our example the input is an array - we will say that the array is of size n. We will use the 'n' to denote input size in our Big-O analysis. So, the real question is how Big-O analysis measures efficiency. Basically, Big-O will want to express how many times the 'n' input items are 'touched'. The word 'touched' can mean different things in different algorithms - in some algorithms it may mean the number of times a constant is multiplied by an input item, the number of times an input is added to a data structure, etc. But in our functions CompareSmallestNumber and CompareToAllNumbers, it just means the number of times an array value is compared to another value. ## Big O notation time complexity In the function CompareSmallestNumber, the n (we used 10 items, but lets just use the variable 'n' for now) input items are each 'touched' only once when each one is compared to the minimum value. In Big O notation, this would be written as O(n) - which is also known as linear time. Linear time means that the time taken to run the algorithm increases in direct proportion to the number of input items. So, 80 items would take longer to run than 79 items or any quantity less than 79. Another way to phrase this is to say that the algorithm being used in the CompareSmallestNumber function has order of n Subscribe to our newsletter for more free interview questions. You might also see that in the CompareSmallestNumber function, we initialize the curMin variable to the first value of the input array. And that does count as 1 'touch' of the input. So, you might think that our Big O notation should be O(n + 1). But actually, Big O is concerned with the running time as the number of inputs - which is 'n' in this case - approaches infinity. And as 'n' approaches infinity the constant '1' becomes very insignificant - so we actually drop the constant. Thus, we can say that the CompareSmallestNumber function has O(n) and not O(n + 1). Also, if we have n ## What is Big O notation worst case? Now, let's do the Big O analysis of the CompareToAllNumbers function. The longest for the CompareToAllNumbers function to run. When does that scenario occur?
Well, let's think about what the worst case running time for the CompareToAllNumbers function is and use that as the basis for the Big O notation. So, for this function, let's assume that the smallest integer is in the very last element of the array - because that is the exact scenario which will take the longest to run since it will have to get to the very last element to find the smallest element. Since we are taking each element in the array and comparing it to every other element in the array, that means we will be doing 100 comparisons - assuming, of course, that our input size is 10 (10 * 10 = 100). Or, if we use a variable "n" to represent the input size, that will be n ## Big O analysis measures efficiency Now, let's compare the 2 functions: CompareToAllNumbers is O(n In an interview, you may be asked what the Big-O of an algorithm that you've come up with is. And even if not directly asked, you should provide that information in order to show that you are well aware of the need to come up with an efficient solution whenever possible. ## What about Big Omega notation?Big O and Big Omega notations are not the same thing. You can read about the differences here: Big O versus Big Omega. |

Justify

why the complexity of all stack methods are O(1)?. Your justification MUST be supported by reliable sources. help me.. email at muazaag@gmail.com

Poor joejoe, if only you could focus and apply as much effort to learning as you do trolling you may be able to understand things such as these

*fear

niceeeee tits

but you still me Me …

poser !!.. itz obvious u did not understang tiss …

wanna piece of tiss …?

Fuck you.

vekoz u is stupid .. unlike mi that have a lot of IQ ….

want some fagg ..?

tis is the worst tut0rial evahh …

I have a lot of IQ … so im not stupeed ….

stop thanking this post you Poser NErds ….

shattap Nerd …..

shattap virgin ….

Bullshit ….

Bullshit

those who that this is helpful …. that’s just BULLSHIT …..

Awesome explanation!!

“Remember that Big-O analysis is used to measure the efficiency of an algorithm based on the time it takes for the algorithm to run as a function of the input size. ”

FYI… it doesn’t use TIME, it’s STEPS. Big O does -not- measure time in any way, fashion or form. Why? Because processors, compiler optimization algorithms, etc., all have “isms” that can improve a functions time over what you can describe mathematically.

So, it’s based on STEPS. Steps = complexity.

Big O does not compare to Function Points. The IFPUG (.org) has been doing this for decades longer than Big O has been a buzz ward, and is far more reliable imho. With complexity measurements using function points, I can tell you not only how complex a function is but how long it’ll take to implement and how much it’ll cost you to do so.

Dr. B

for(icount2=1;icount<=n;icount2=icount2*2)

{

for(icount3=1;icount3<=n;icount3=icount3*2)

{

……

…..}

}

}

what is the time complexity of this code????????

please do reply

thnku

Nice explanation. Good

Nice One. Now i understood the whole concept clearly..!!!

Nice like it….. Thanks

amazing tutorials! 😀

thanks a lot for this!

I like it but I tend to dislike ‘syntaxy’ type questions. I know MANY amazing programmers with an innate ability to create tight, highly efficient algorithms but ask them about ‘Big O Notation’ and they will literally draw a blank – as-in: they’ll look at you like an alien. You see this alot in college (vs university) educated and/or self-taught techs – myself included.

Because of this fact I would rather ask them to SOLVE a problem, then explain to me in their words WHY they chose that solution. In the end, they will give an explanation similar to yours, without ever mentioning this specific jargon (i.e. they’ll explain how the alternative requires 100,000 loops vs. their 10,000 without mention of Big O Notation).

It’s for this same reason that I tend to look beyond syntax when quizing. As I see it, any monkey can memorize syntax, but it requires a brain to logically approach and solve problems!

awsum Thx so much

Very Useful..Thanks you!!!

I needed to review this for my class this summer. Thanks.

I didn’t even understand a thing

You made me realize how stupid I was to think that big O is some kind of rocket science. Thank you, very well explained, love the way you present your case.

fantastic, just fantastic. it really has helped me. Keep up!

not only extremely useful but pleasant to read

amazing site

wit <3 from russia

good explanation….

Nice tutorial. Now I don’t fear of Big O any more.

Clear explanation .. like it !!

niceeeeeeeeeee

A vivid explanation that I never heard off. Gratitude to you

You're right Nick, it's been fixed. Thanks!

this is wrong "Big O Notation, also known as Big Omega Notation" Big O is upper bound while Big Omega is lower bound

Dude.. Thanks for this clear explanation makes more sense now to me!

amazing explanation..thanks for this article

I probably will attend an technical interview next week, and it is so lucky that I found this website, very useful information.

thanks….this is very useful…..i always visit this site and wow..i like the tutorials…very easy to understand and straight forward

thank you sir….it is so use full….clear about the all the things…!

very useful…keep going..:)

got everything right…… it's really very useful!!!!!!!!

Very helpful for initial understanding

Glad it helped you, thanks Sumit!

Very useful tutorial.Thank you very much Sir.