title: Insertion Sort by swapping
body: <p>I've just started to DSA, and had a question about insertion sort.</p>
  
  <p>This the version from textbooks / tutorials.</p>
  
  <pre><code>void insertion_sort(int A[], int n) {
      for (int i = 0; i &lt; n; i++) {
          int temp = A[i];    
          int j = i;
  
          while (temp &lt; A[j - 1] &amp;&amp; j &gt; 0) {
              A[j] = A[j - 1];   
              j = j - 1;
          }
          A[j] = temp;       
      }  
  }
  </code></pre>
  
  <p>I was thinking would it make a difference, if we used <code>swapping</code> numbers instead of shifting the numbers and inserting the <code>temp</code> value in the correct hole position.</p>
  
  <pre><code>void insertionSort(int A[], int n) {
      for (int i = 0; i &lt; n; i++) {
          int temp = A[i];
          int j = i;
  
          while (temp &lt; A[j - 1] &amp;&amp; j &gt; 0) {
              swap(A[j], A[j - 1]);
              j--;
          }
      }
  }
  </code></pre>
  
  <p>Swap Code:</p>
  
  <pre><code>void swap(int &amp;a,int &amp;b){
      int temp = a;
      a = b;
      b = temp;
  }
  </code></pre>
  
  <p>Oh, and it would be really awesome if someone could explain the Time Complexities of both.</p>