title: Counting the number of comparisons in quicksort
body: <p>I have a homework where I need to implement quicksort with three partition strategies and count the number of comparisons for each strategy.</p>
  
  <p>for simplicity, we are asked to add <code>m-1</code> to the count every time we do a recursive call on an array of length  <code>m</code>.</p>
  
  <p>my code is always returning a negative number and  it is not an integer overflow issue.<br>
  I used <code>long long int</code> and I still have it, and there is no way the number of comparisons would grow that much, so there is something wrong with the way I am counting.</p>
  
  <p>I tested the code on a 100000 element array using <code>is_sorted</code> after calling my implementation and it passed, so the the sorting is correct<br>
  here is my code:</p>
  
  <pre><code>long quick_sort (vector &lt;int&gt;&amp; A, int l , int r){
      static  long count = 0;
      if ( r&lt;= l)
          return 0;
  
      //partition
      int i = partition(A, l, r);
  
      //quicksort left
      int amount = ( ((i -1) -l) &gt;= 0 ?
                              ((i-1) -l) :
                                      0);
      count += amount;
      quick_sort (A,l, i-1);
  
      //quicksort right
      amount = ((r - (i +1)) &gt;= 0 ?
                          (r - (i +1)) :
                                    0);
      count += amount;
      quick_sort (A,i+1, r);
  
      return count;
  
  }
  </code></pre>