Cracking the Code of Quadratic Time Complexity: A Comprehensive Guide to Understanding Big O Notation for N² Algorithms
Table of contents
Preview
Before we jump out to what is quadratic times, first you have to see and understand the chunk of code below
fun printStars(numberOfStars: Int){
val lines = 15
for (j in 1 until lines){
for (i in 1 until numberOfStars){
println("*")
}
println()
}
}
One thing and the most important we have to notice in the code above is there are nested loops, there is more than one loop on the printStarts function.
There are the rule is that every time you see nested loops, that automatically tells you that we have Big O of n quadratic, which is a quadratic function so we can continue to count the other things. why it is quadratic time? because when you look and the function there is two input array that does not define the value at the first, that is mean the operations will take time depending on how much input we insert, if we have for loop with that condition, that is mean every time there is for loop with depend on the input we also have Big O(n). we can break down the code like this, will put the note of Big O in the comment section of the coding below:
fun printStars(numberOfStars: Int){
val lines = 15 // Big O (1)
for (j in 1 until lines){ // Big O (n)
for (i in 1 until numberOfStars){ // Big O (n)
println("*")
}
println()
}
}
If we calculated it
Big O (1 + n n ) = n\n or n quadratic*
you can go to the original page from this image here: https://jarednielsen.com/big-o-quadratic-time-complexity/
Based on the chart, we can set it at n quadratic is have the input zero first and is increasingly extremely complex.