title: Number of swaps in Bubble Sort
body: <p>I have a version of bubble sort:</p>
  
  <pre><code>int i, j;  
  
  for i from n downto 1 
  {
      for j from 1 to i-1 
      { 
          if (A[j] &gt; A[j+1])
              swap(A[j], A[j+1]) 
      } 
  }
  </code></pre>
  
  <p>I want to calculate the expected number of swaps using the above version of bubble sort. The method used by me is shown below :</p>
  
  <pre><code>// 0 based index
  
  float ans = 0.0;
  
  for ( int i = 0; i &lt; n-1; i++ )
  {
      for ( int j = i+1; j &lt; n; j++ ) {
  
          ans += getprob( a[i], a[j]); // computes probability that a[i]&gt;a[j].
      }
  }
  </code></pre>
  
  <p>Am i going the correct way or am I missing something?</p>