vendredi 23 mars 2018

Complexity of Algorithm (with if and else)

func3(int n) {
    for (int i = 1; i<n; i++)
        System.out.println("*");
    if (n <= 1)
    {
        System.out.println("*");
        return;
    }
    if(n % 2 != 0) //check if odd
        func3(n - 1)
    else
    func3(n / 2);
    return;
}

I need to calculate the complexity of this algorithm i tried divide it to 2 cases: best case that is only 1 call excecute when the n is even(2^i number) and the complexity will T(n)=T(n/2)+O(n). Someone can give me some help with the worst case? and write here the answer please

Aucun commentaire:

Enregistrer un commentaire