String joinWords (String[] words){
String sentence = "";
for(String w: words){
sentence = sentence + w;
}
return sentence;
}
The book reports this to be O(xn^2)
Here is my working:
1 call to create String sentence
originally
There are N calls (due to N calls for the for loop)
then there are N calls to assign sentence = sentence + w
Last call to send return sentence;
Total:
Which gives O(N^2 + 2) = O(N^2)
Questions (1) Is my working correct?
(2) Where do they get the extra factor of x
in O(xn^2)
?
Thanks!