What is the complexity of the below code with respect to memory ?
- by Cshah
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 ?