What is the complexity of the below code with respect to memory ?

Posted by Cshah on Stack Overflow See other posts from Stack Overflow or by Cshah
Published on 2010-03-28T13:29:46Z Indexed on 2010/03/28 13:33 UTC
Read the original article Hit count: 449

Hi, I read about Big-O Notation from here and had few questions on calculating the complexity.So for the below code i have calculated the complexity. need your inputs for the same.

    private void reverse(String strToRevers) 
    {
        if(strToRevers.length() == 0)
        {
            return ;
        }
        else 
        {
            reverse(strToRevers.substring(1));
            System.out.print(strToRevers.charAt(0));
        }
    }

If the memory factor is considered then the complexity of above code for a string of n characters is O(n^2). The explanation is for a string that consists of n characters, the below function would be called recursively n-1 times and each function call creates a string of single character(stringToReverse.charAT(0)). Hence it is n*(n-1)*2 which translates to o(n^2). Let me know if this is right ?

© Stack Overflow or respective owner

Related posts about big-o

Related posts about memory