0

I am trying to find the Big-O for the Summing function. I know the two loops usually mean that N^2 run time but this is just one N but j is running many more than N itself.

int Summing( int n )
{
    int i 
    int j
    int s = 0;
    for(i=0; i < n; i++){
        for(j=0; j < i; j++) {
            s += i*i;
          }
    }
khelwood
  • 55,782
  • 14
  • 81
  • 108

1 Answers1

1

You can calculate the exact time the inner loop takes as a function of i and then sum it up over all values that i takes.

Here the number of times the innermost section is run is 0 + 1 + 2 + ... + (n-1) = (n-1)*n/2 = (n^2)/2 - n/2 which is O(n^2)

Gavin Haynes
  • 1,721
  • 11
  • 21