title: Array Sorting Performance
body: <p>This is a pretty simple, straightforward problem, but, of course, I've managed to do something wrong.  First, I generated 5 different arrays of 10 random numbers--from 1 to 10, 1 to 100, up to 1 to 100,000.  Then I took each array and performed 5 different types of sorts (for a total of 25), calculating the time it takes to perform the sorts.  I cannot figure out why each and every result is 0ms regardless of the size of n.  What am I doing wrong?</p>
  
  <pre><code>public class Lab16Sorting {
  
  public static void main(String[] args) 
  {
      final int TOTAL_NUMBERS = 10;
      int count;
      int[] num = new int[TOTAL_NUMBERS];
      Random rand = new Random();
  
      // Generate 10 numbers from 1 - 10  
      System.out.println("SORT 10");
      System.out.println("----------------");
  
      for (count = 0; count &lt; TOTAL_NUMBERS; count++)
          num[count] = rand.nextInt(10);
  
      System.out.println("Array: " + num);
      runSort(num);
  
      // Generate 10 numbers from 1 - 100     
      System.out.println("\nSORT 100");
      System.out.println("----------------");
  
      for (count = 0; count &lt; TOTAL_NUMBERS; count++)
          num[count] = rand.nextInt(100);
  
      System.out.println("Array: " + num);
      runSort(num);
  
      // Generate 10 numbers from 1 - 1,000       
      System.out.println("\nSORT 1,000");
      System.out.println("----------------");
  
      for (count = 0; count &lt; TOTAL_NUMBERS; count++)
          num[count] = rand.nextInt(1000);
  
      System.out.println("Array: " + num);
      runSort(num);
  
      // Generate 10 numbers from 1 - 10,000  
      System.out.println("\nSORT 10,000");
      System.out.println("----------------");
  
      for (count = 0; count &lt; TOTAL_NUMBERS; count++)
          num[count] = rand.nextInt(10000);
  
      System.out.println("Array: " + num);
      runSort(num);
  
      // Generate 10 numbers from 1 - 100,000     
      System.out.println("\nSORT 100,000");
      System.out.println("----------------");
  
      for (count = 0; count &lt; TOTAL_NUMBERS; count++)
          num[count] = rand.nextInt(100000);
  
      System.out.println("Array: " + num);
      runSort(num);
  }
  
  /**
   * Run sort algorithms
   */
  
  private static void runSort(int[] num)
  {
      long before;
      long after;
  
      // Run and display selection sort
      before = System.currentTimeMillis();
      selectionSort(num);     
      after = System.currentTimeMillis();
      System.out.println("Selection sort took "+ (after-before) +" milliseconds");
  
      // Run and display bubble sort
      before = System.currentTimeMillis();
      bubbleSort(num);
      after = System.currentTimeMillis();
      System.out.println("Bubble sort took "+ (after-before) +" milliseconds");
  
      // Run and display insertion sort
      before = System.currentTimeMillis();
      insertionSort(num);     
      after = System.currentTimeMillis();
      System.out.println("Insertion sort took "+ (after-before) +" milliseconds");
  
      // Run and display merge sort
      before = System.currentTimeMillis();
      mergeSort(num);
      after = System.currentTimeMillis();
      System.out.println("Merge sort took "+ (after-before) +" milliseconds");
  
      // Run and display quick sort
      before = System.currentTimeMillis();
      quickSort(num);
      after = System.currentTimeMillis();
      System.out.println("Quick sort took "+ (after-before) +" milliseconds");        
  }
  </code></pre>
  
  <p>I printed out the various array addresses and I see they're all the same (which makes sense since I'm using the same array object).  I thought that was the problem and so I tried using different arrays (<code>int[] num</code>, <code>int[] num2</code>...) and I tried re-initializing the array after each <code>runSort()</code> method call with <code>num = new int[TOTAL_NUMBERS]</code>.</p>