Ivan
Ivan

Reputation: 427

time complexity, Java

Please see the code that I wrote based on a school example.

public class Test {
public static void main(String [] args)
{
    int number = 0;
    int [] array = new int[number+1];
    array[number] = 0;
    methodName(number, array);
}

public static void methodName(int n, int[] b )
{
    if (n == 0)
    {
        System.out.println(" b is : " + b);
        return;
    }
    else
    {
        b[n-1] = 0;
        methodName(n-1, b);
        b[n-1] = 1;
        methodName(n-1, b);
    }
}
}

I am trying to calculate the best and worst case time complexity of this code. As far as I understand the best case would be O(1). And I'm having a difficulty determining the worst case. There are four basic operations in the else loop. I know that this is a progressively growing function and I have a feeling it is close to being O(!n).

Thank you for your time.

Upvotes: 0

Views: 150

Answers (1)

Vivek Kamble
Vivek Kamble

Reputation: 106

IF methodName is not getting called from anywhere else than main, then it would always be O(1)

Upvotes: 1

Related Questions