调用另一个函数时计算算法的复杂性

时间:2017-02-03 18:26:07

标签: java big-o

private static int f(int[] a, int low, int high)
{
    int res = 0;
    for (int i = low; i <= high; i++)
        res += a[i];
    return res;
}

/**
 *  
 * @return the size of the largest gap in the array in which the combined values contained in the indexes are divisible by 3 
 * 
 */
public static int what(int[] a)
{
    int temp = 0;
    for (int i = 0; i < a.length; i++)
    {
        for (int j = i; j < a.length; j++)
        {
            int c = f(a, i, j);
            if (c % 3 == 0)
            {
                if (j - i + 1 > temp)
                    temp = j - i + 1;
            }
        }
    }
    return temp;
}

我需要计算what的算法复杂度。我认为它可能是n^2,因为它有两个循环,但它也可能是n^3,因为它使用f,它有另一个循环。如何确定其算法复杂度?

0 个答案:

没有答案