title: Insertion Sort from T Cormen Book
body: <p>I am working through the "Introduction to Algorithms" book by Cormen, and I have created the following from pseudocode. However, the first two elements of the Array do not seem to be sorted. I cannot spot the error (possibly because its late). So I was wondering if anybody could see from first glance.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  
  using namespace std;
  
  int main(){
    int input;
    cout &lt;&lt; "Enter length of desired array." &lt;&lt; "\n";
    cin &gt;&gt; input;
    cout &lt;&lt; "\n";
  
    int A [input];
  
    //Populate and print the Array.
    for(int i=0; i&lt;input; i++){
      A[i] = rand()%99-1;
      cout &lt;&lt; A[i] &lt;&lt; " ";
    }
  
    cout &lt;&lt; "\n";
  
    //Insertion sort.
    for(int j=2; j&lt;input; j++){ //Iterate through the Array.
      int key = A[j]; //Store the current element into key.
      int i = j-1; //Iterator for while loop.
      while(i&gt;0 &amp;&amp; A[i]&gt;key){ //Loop to insert A[j] into the sorted sequence.
        A[i+1] = A[i]; //Move the element.
        i=i-1; //New value of i.
        A[i+1] = key; //Update the key
      }
    }
  
    for(int i=0; i&lt;input; i++){
      cout &lt;&lt; A[i] &lt;&lt; " ";
    }
    return 0;
  }
  </code></pre>