title: SelectionSort and BubbleSort - how to count number of comparisons and number swapping?
body: <p>First of all, I have seen a similar question relating to C++, but I didn't quite understand it - plus my question is about Java.</p>
  
  <p>Basically I have coded two methods that can use SelectionSort and BubbleSort on an array parsed in. While I believe I have the methods working correctly (I have run tests and they all have sorted the numbers in ascending order), I am not sure if I am counting the number of comparisons and number swaps correctly. If someone is able to test my code below and offer some feedback, I will be very grateful.</p>
  
  <p>Note: I can zip up my Java project files and send them to anyone if needed.</p>
  
  <p><strong>BubbleSort method:</strong></p>
  
  <pre><code>public String bubbleSort(int[] numbers)
      {
          System.out.println("******|Bubble Sort|******");
          StringBuilder originalArray = new StringBuilder();
  
          for(int i = 0; i &lt;= numbers.length - 1; i++)
          {
              originalArray.append(numbers[i] + " ");
          }
          System.out.println("Original array: " + originalArray);
          int temp; // temporary variable
  
          //Set boolean variable to true, 
          //to allow the first pass.
          boolean pass = true;
  
          int comparisons = 0;
          int swaps = 0;
  
          //While a pass can be made, 
          while(pass)
          {
              //Set the boolean value to false, 
              //indicating a number swap could 
              //be made.
              pass = false;
  
              for(int i = 0; i &lt; numbers.length - 1; i++)
              {
                  //increment the number of comparisons by 1.
                  comparisons++;
                  if(numbers[i] &gt; numbers[i+1])
                  {
                      temp = numbers[i];
                      numbers[i] = numbers[i + 1];
                      numbers[i+1] = temp;
  
                      //increment the amount of swaps made by 1, 
                      //to put numbers in correct order.
                      swaps++;
                      pass = true;
                  }
              }
          }
  
          //Create a StringBuilder object - to hold 
          //the output of sorted numbers.
          StringBuilder sb = new StringBuilder();
  
          //Loop through the now sorted array - appending 
          //each subsequent number in the array to the 
          //StringBuilder object.
          for(int i = 0; i &lt; numbers.length; i++)
          {
              sb.append(numbers[i] + " ");
          }
  
          //Return the final results of the sorted array.
          return "Sorted Array (asc): " + sb.toString() + "\nComparisons made: " + comparisons 
                  + "\nSwaps made: " + swaps;
      }
  </code></pre>
  
  <p><strong>SelectionSort method</strong></p>
  
  <pre><code>public String selectionSort(int[] numbers)
      {
          System.out.println("******|Selection Sort|******");
          StringBuilder originalArray = new StringBuilder();
  
          int comparisons = 0;
          int swaps = 0;
  
          for(int i = 0; i &lt;= numbers.length - 1; i++)
          {
              originalArray.append(numbers[i] + " ");
          }
          System.out.println("Original array: " + originalArray);
  
          //Declare variable to hold first element
          int first;
  
          //declare temporary variable, to be used in 
          //swapping integers.
          int temp;
  
          for(int x = numbers.length - 1; x &gt; 0; x--)
          {
              first = 0;
              comparisons++;
              for(int y = 1; y &lt;= x; y++)
              {
                  //comparisons++;
                  if(numbers[y] &gt; numbers[first])
                  {
                      first = y;
                      //comparisons++;
                      swaps++;
                  }
                  temp = numbers[first];
                  numbers[first] = numbers[x];
                  numbers[x] = temp;
                  //swaps++;
              }
          }
  
          //Create a StringBuilder object - to hold 
          //the output of sorted numbers.
          StringBuilder sb = new StringBuilder();
  
          //Loop through the now sorted array - appending 
          //each subsequent number in the array to the 
          //StringBuilder object.
          for(int i = 0; i &lt; numbers.length; i++)
          {
              sb.append(numbers[i] + " ");
          }
  
          //Return the final results of the sorted array.
          return "Sorted Array (asc): " + sb.toString() + "\nComparisons made: " + comparisons 
                  + "\nSwaps made: " + swaps;
      }
  </code></pre>