For simplicity we have a matrix that has same width(n) as height(m). Or you could say same number of columns(n) as rows(m). That means m=n and we don’t have to differentiate between them.

First for cycle is same like example 2.

Second for cycle is different because it will be executed n-times, because it is inside for cycle that has size of n. => n(3(n)+1)

And also everything that is inside second for cycle will be executed n-times. => n(2(n))

When we solve the equation the result is: 3 + 4n + 5n^2

Time taken by this algorithm is quadratic.

Details:

My demo contains methods that have detail suffix like TotalDetail. These methods are the same like the methods without suffix however these methods count total units of time. With this functionality you can check your equation result.

So for example our linear example has equation: 5n + 3.

And we know that n = array length. For array lenght = 10, the number of operations must equal 5 * 10 + 3 = 53 units of time.

If you try the demo with different numbers the result of equatuon will always match the number of operations.

Don’t forget that for quadratic equation you have to use matrix that has same number of columns as rows!

For example: n = m = 3.

Result will be: 3 + 4*3 + 5*(3)^2 = 3 + 12 + 45 = 60 units of time.

Summary

Growth of these methods are:

Constant: 2 => O(1)

Linear: 5n + 3 => O(n)

Quadratic: 3 + 4n + 5n^2 => O(n^2)

I mentioned O in my last post, it is called big-oh and it basically tell us aproximately what function our algorithm will copy. I will talk more about big-oh in my next post.