title: Insertion sort: count swaps and comparisons
body: <p>For some reason I can't get the count for comparisons and swap in the InsertionSort part, it just outputs zero. And when I isolate the code for it, it outputs a number of swaps and comparisons (though I don't know if it's wrong or not, probably wrong considering the number is the same for both) and the array is not sorted at all. I'm really confused as to why this isn't working, any help is greatly appreciated!</p>
  
  <p>Update: The instance of bubble was being passed onto both selection and insertion, now that that's fixed turns out T also have a problem with the selection part. Any suggestions on how to fix them?</p>
  
  <p>Update 2: Fixed the selection part! Still confused about insertion.</p>
  
  <pre><code>import java.util.Scanner;
  
  public class Sorting {
  
  public static void main(String[] args) {
  
      int n, c;
  Scanner scan = new Scanner(System.in);
  
  System.out.print("Number of elements: ");
  n = scan.nextInt();
  int[] bubbleSortArray = new int[n];
  int[] selectionSortArray = new int[n];
  int[] insertionSortArray = new int[n];
  
  System.out.print("Enter " + n + " elements: ");
  
  for (c = 0; c &lt; n; c++) {
      int i = scan.nextInt();
      bubbleSortArray[c] = i;
      selectionSortArray[c] = i;
      insertionSortArray[c] = i;
  }
  
  BubbleSort(bubbleSortArray);
  SelectionSort(selectionSortArray);
  InsertionSort(insertionSortArray);
  
  }
  
  
  static void BubbleSort(int[] array) {
  
      int n = array.length;
      int cm = 0;
      int sw = 0;
  
      for (int c = 0; c &lt; (n - 1); c++) {
          for (int d = 0; d &lt; n - c - 1; d++) {
              cm++;
              if (array[d] &gt; array[d + 1]) {
                  int swap = array[d];
                  array[d] = array[d + 1];
                  array[d + 1] = swap;
                  sw++;
              }
          }
      }
  
      System.out.print("Bubble sort: ");
  
      for (int c = 0; c &lt; n; c++) {
          System.out.print(array[c] + " ");
  
  
      }
      System.out.println("- " + cm + " comparisons, " + sw + " swaps");
  }
  
  static void SelectionSort(int[] array) {
  
       int n = array.length;
       int cm = 0;
       int sw = 0;
  
       for (int c = 0; c &lt; n - 1; c++) {
              int index = c;
              for (int d = c + 1; d &lt; n; d++){
                  cm++;
                  if (array[d] &lt; array[index])
                      index = d;
              }
              int temp = array[index]; 
              sw++;
              array[index] = array[c];
              array[c] = temp;
          }
          System.out.print("Selection sort: ");   
          for (int c = 0; c &lt; n; c++) {
              System.out.print(array[c] + " ");
  
              }
          System.out.println("- " + cm + " comparisons, " + sw + " swaps");
          }
  
  static void InsertionSort(int[] array) {
  
      int n = array.length;
      int cm = 0;
      int sw = 0;
  
      for (int c = 1; c &lt; n; c++){
          int temp = array[c];
          for (int d = c - 1; d &gt; 0 &amp;&amp; temp &lt; array[d]; d--) {
              array[d+1] = array[d];
              array[d+1] = temp;
              cm++;
              sw++;
  
          }
      }
      System.out.print("Insertion sort: ");   
      for (int c = 0; c &lt; n; c++) {
          System.out.print(array[c] + " ");
  
  
  }
      System.out.println("- " + cm + " comparisons, " + sw + " swaps");
      }
  }
  </code></pre>