title: How to count swaps in bubble sort?
body: <p>I am trying to write a program to count only how many swaps and calculates the check-sum of the sorted array. However, I am not sure why the counter is not getting the correct swaps! I have traced it many times, this should be working perfectly fine. What am I missing?</p>
  
  <p>The program works buy putting only a positive numbers as an input for the array, to  terminate the program and display the swaps you should enter "-1"</p>
  
  <p>a simple example of an input of:</p>
  
  <blockquote>
    <p>1 4 3 2 6 5 -1</p>
  </blockquote>
  
  <p>the output should be</p>
  
  <blockquote>
    <p>3</p>
  </blockquote>
  
  <p>but the output is not correct at all. </p>
  
  <pre><code>#include"stdafx.h"
  #include &lt;iostream&gt;
  using namespace std;
  
  void bubble_sort(unsigned long long int arr[], int n)
  {
      int swaps=0;
      for (int i = 0; i &lt; n-1; ++i) {
          for (int j = 0; j &lt; n - i - 1; ++j) {
              if (arr[j] &gt; arr[j + 1]) {
                  int temp = arr[j];
                  arr[j] = arr[j + 1];
                  arr[j + 1] = temp;
                  swaps++;
              }
          }
      }
  
      cout &lt;&lt; "Swaps: " &lt;&lt; swaps &lt;&lt; endl;
  }
  
  int main() {
      int count=0;
      unsigned long long int  input_ar[1000];
      cout &lt;&lt; "Enter the numbers: " &lt;&lt; endl;
      for (int i = 0; i &lt; 1000; i++) {
          cin &gt;&gt; input_ar[i];
          if (input_ar[i] == -1) {
              break;
          }
          count++;
      }
  
      bubble_sort(input_ar, count);
      return 0;
  }
  </code></pre>