The ‘Big O’ Champion – Part 2
Welcome back! Are you all excited for the second course – appetizer? Last time, we enjoyed the first course and in case you missed it, DON'T LEAVE! Here is the link: https://adyatech.hashnode.dev/the-big-o-champion-part-1
Go on, enjoy, and then please do come back as the second course is waiting for you… So, shall we start? 😁
Remember the last time we met; I introduced ‘Big O’ to you. Well today, I will tell you about one of its basic complexities – O(n). As the other three complexities follow almost the same concept of O(n), I shall explain it to you first.
What is o(n)?
If you go by the technical definition of O(n), it says:
O(n) represents an algorithm that will take on the order of ‘n’ operations to insert an item.
Pretty confusing right? 😥 Let’s break down this complicated definition and understand it part by part!
Below is a simple Java code to help you:
The n in O(n) refers to the size of the input. So, in the code above, we have given the value of n as 10. This means that the entire code will run 10 times and display an output each time.
Hence, the output will be:
Wait! How did the code work? Well, here are the working steps:
A ‘for loop’ started with counter i initialized at 0.
Since n=10, i<n will be 0<10.
This will increment i by 1.
Now, i that is 1 will be printed on screen.
This loop will continue as long as i stays less than 10. And that's how the concept of O(n) works. It receives a value of n and then starts a loop ‘n’ number of times, each time printing out the result.
Aaaah! It's the end of the appetizer but…. the main course is all ready and waiting to be served! While I get it for u, do drop a ❤️ as your feedback means the world to me. Remember, we are a part of the enthusiastic learner community and together we will become the Big O Champions! Till then…
Learn, Share, Grow!
Bye!