Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computing Algorithm Complexity - Confusion

I have the following code snippet:

sum = 0;
for (i = 0; i < n; i++)
    for (j = 0; j < i; j++)
        sum++;

The complexity would be O(n^2), but if I want to dig a little more for the internal loop complexity then would it be (n (n-1))/2 or (n-1)!?

like image 610
x.509 Avatar asked Apr 01 '26 03:04

x.509


1 Answers

Yes, O(n^2), but actually 0+1+...+n-1=n(n-1)/2 = O(n^2), definitely not (n-1)!

like image 176
Ramashalanka Avatar answered Apr 03 '26 16:04

Ramashalanka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!