I have created two algorithms that calculate the prefix averages with a given array. I wanted to derive the time complexities of both algorithms, but I have been struggling a bit. I watched this YouTube video: https://www.youtube.com/watch?v=udwxWq9wZgg&safe=active. I did not understand how to count the operations in a for loop and a nested for loop.
At 2:27, I managed to count the operations in the for loop in PrefixAverages2. It was 3n+1. However, I cannot understand from 5:50 onwards.
Thanks in advance.
public double[] PrefixAverages1(double input[])
{
double A[] = new double[input.length];
double s;
for(int i=0; i <= input.length - 1 ;i++)
{
s = input[0];
for(int j=1; j <= i ;j++)
{
s = s + input[j];
}
A[i] = s / (i+1);
}
return A;
}
public double[] PrefixAverages2(double input[])
{
double A[] = new double[input.length];
double s = 0;
for( int i=0; i <= input.length - 1 ; i++)
{
s = s + input[i];
A[i] = s / (i+1);
}
return A;
}