title: What is the complexity of this simple piece of code?
body: <p>I'm pasting this text from an ebook I have. It says the complexity if O(n<sup>2</sup>) and also gives an explanation for it, but I fail to see how.</p>
  
  <p>Question: What is the running time of this code?</p>
  
  <pre><code>public String makeSentence(String[] words) {
      StringBuffer sentence = new StringBuffer();
      for (String w : words) sentence.append(w);
      return sentence.toString();
  }
  </code></pre>
  
  <p>The answer the book gave:</p>
  
  <blockquote>
    <p>O(n<sup>2</sup>), where n is the number of letters in sentence.  Here’s why: each time you
    append a string to sentence, you create a copy of sentence and run through all the letters in
    sentence to copy them over If you have to iterate through up to n characters each time in the
    loop, and you’re looping at least n times, that gives you an O(n<sup>2</sup>) run time. Ouch!</p>
  </blockquote>
  
  <p>Can someone please explain this answer more clearly?</p>