Advertisement
brilliant_moves

TestBubbleSort2.java

Feb 5th, 2015
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.90 KB | None | 0 0
  1. public class TestBubbleSort2 {
  2.  
  3.     /**
  4.     *   Program:    TestBubbleSort2.java
  5.     *   Purpose:    Test running time of bubble sort with sorted versus unsorted arrays.
  6.     *   Creator:    Chris Clarke
  7.     *   Created:    05.02.2015
  8.     */
  9.  
  10.     static final int MAX=100000; // a hundred thousand
  11.  
  12.     public static int[] initSortedArray() {
  13.         int[] theArray = new int[MAX];
  14.         for (int i=0; i<theArray.length; i++) {
  15.             theArray[i] = i;
  16.         } // for
  17.  
  18.         return theArray;
  19.     } // initSortedArray()
  20.  
  21.     public static int[] getShuffledArray(int min, int max) {
  22.  
  23.         /**
  24.         *   max is the array size
  25.         *   when min=0, range is 0 to max-1
  26.         *   shuffle or rearrange the array values
  27.         */
  28.            
  29.         int[] theArray, results;
  30.         theArray = new int[max];
  31.         results = new int[max];
  32.  
  33.         int r;  // a random number
  34.  
  35.         // initialise the array
  36.         for (int i=0; i<max; i++)
  37.             theArray[i]=i+min; // when min=1, range is 1 to max inclusive
  38.                        // when min=0, range is 0 to max-1 inclusive
  39.  
  40.         // pick n numbers
  41.         for (int i=0; i<max; i++) {
  42.             // range 1 to 49, 1 to 48 etc
  43.             r = (int) ((max-i) * Math.random());
  44.             // store results
  45.             results[i] = theArray[r];
  46.             // move last number in theArray to position r
  47.             theArray[r] = theArray[max-1-i];
  48.         } // end for
  49.  
  50.         return results;
  51.  
  52.     } // end getShuffledArray()
  53.  
  54.    
  55.     public static int[] sort(int[] a) {
  56.     // apply bubblesort algorithm to array a
  57.         int i, j, temp;
  58.         int len = a.length;
  59.         boolean swaps; // optimise code: when no swaps, array is sorted
  60.  
  61.         for (i=0; i<len-1; i++) {
  62.             swaps = false;
  63.             for (j=0; j<len-1-i; j++) {
  64.                 if (a[j]>a[j+1]) {
  65.                     // swap
  66.                     temp=a[j];
  67.                     a[j]=a[j+1];
  68.                     a[j+1]=temp;
  69.                     swaps = true;
  70.                 } // if
  71.             } // for j
  72.             if (!swaps) break; // from outer loop
  73.         } // for i
  74.  
  75.         return a;
  76.     } // sort()
  77.  
  78.     public static void main(String[] args) {
  79.         NanoTimer timer = new NanoTimer();
  80.         int[] sorted = initSortedArray();
  81.         int[] unsorted = getShuffledArray(0, MAX);
  82.         System.out.println("Array size: "+sorted.length);
  83.         System.out.println("Sorting sorted array...");
  84.         timer.setStartTime();
  85.         sorted = sort(sorted);
  86.         timer.setStopTime();
  87.         System.out.println("Sorted.  Running time: "+timer.toString());
  88.         System.out.println("Sorting unsorted array...");
  89.         timer.setStartTime();
  90.         sorted = sort(unsorted);
  91.         timer.setStopTime();
  92.         System.out.println("Sorted.  Running time: "+timer.toString());
  93.     } // main()
  94.  
  95. } // class TestBubbleSort2
  96.  
  97. class NanoTimer {
  98.  
  99.     /**
  100.     *   Class:      NanoTimer.java
  101.     *   Purpose:    Get program running times in nanoseconds
  102.     *   Creator:    Chris Clarke
  103.     *   Created:    03.04.2014
  104.     */
  105.  
  106.     private long startTime = 0, runningTime = -1;
  107.  
  108.     public void setStartTime() {
  109.         startTime = System.nanoTime();
  110.     }
  111.  
  112.     public void setStopTime() {
  113.         runningTime = System.nanoTime() - startTime;
  114.     }
  115.  
  116.     public String toString() {
  117.         if (runningTime == -1)
  118.             return "Error in running time.";
  119.  
  120.         return runningTime+" nanoseconds";
  121.     }
  122.  
  123. } // class NanoTimer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement