Advertisement
brilliant_moves

TestQuicksort.java

Dec 7th, 2014
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.15 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Arrays;
  3.  
  4. public class TestQuicksort {
  5.  
  6.     /**
  7.     *   Program:    TestQuicksort.java
  8.     *   Purpose:    Test quicksort algorithm by applying it to a shuffled array.
  9.     *   Creator:    Chris Clarke
  10.     *   Created:    07.02.2014
  11.     *   Modified:   07.12.2014 - added NanoTimer; added bubblesort for comparison.
  12.     */
  13.  
  14.     static final int MAX = 100000; // a hundred thousand
  15.  
  16.     static int[] theArray = new int[MAX];
  17.  
  18.     public static void main(String[] args) {
  19.  
  20.         // to time sort algorithm
  21.         NanoTimer timer0 = new NanoTimer();
  22.         NanoTimer timer1 = new NanoTimer();
  23.         NanoTimer timer2 = new NanoTimer();
  24.         NanoTimer timer3 = new NanoTimer();
  25.  
  26.         // initialise array
  27.         for (int i=0; i<MAX; i++) {
  28.             theArray[i] = i;
  29.         } // end for
  30.  
  31.         System.out.println("\nShuffling array...");
  32.         timer0.setStartTime();
  33.         randomiseArray();
  34.         timer0.setStopTime();
  35.  
  36.         System.out.println("Applying quicksort algorithm...");
  37.         timer1.setStartTime();
  38.         quickSort();
  39.         timer1.setStopTime();
  40.  
  41.         timer2.setStartTime();
  42.         display();
  43.         timer2.setStopTime();
  44.  
  45.         System.out.println("\nShuffling array...");
  46.         randomiseArray();
  47.         System.out.println("Applying bubblesort algorithm...");
  48.         timer3.setStartTime();
  49.         bubbleSort();
  50.         timer3.setStopTime();
  51.  
  52.         //display();
  53.  
  54.         System.out.println("Size of array: "+MAX);
  55.         System.out.println("Time to shuffle: "+timer0.toString());
  56.         System.out.println("Time to quicksort: "+timer1.toString());
  57.         System.out.println("Time to bubblesort: "+timer3.toString());
  58.         System.out.println("Time to display result: "+timer2.toString());
  59.     } // end main()
  60.  
  61.     public static void display() {
  62.         System.out.println( Arrays.toString( theArray));
  63.     } // display
  64.  
  65.     public static void randomiseArray() {
  66.         Random r = new Random();
  67.         int ran0, ran1, temp;
  68.  
  69.         for (int i=0; i<MAX; i++) {
  70.             // choose random number
  71.             ran0 = r.nextInt(MAX);
  72.  
  73.             // ensure ran0 and ran1 different
  74.             do {
  75.                 // choose random number
  76.                 ran1 = r.nextInt(MAX);
  77.             } while (ran1 == ran0);
  78.  
  79.             // swap positions in array
  80.             temp = theArray[ran0];
  81.             theArray[ran0] = theArray[ran1];
  82.             theArray[ran1] = temp;
  83.         } // end for
  84.     } // end randomiseArray()
  85.  
  86.     public static int partition(int[] arr, int left, int right) {
  87.         int i = left, j = right;
  88.         int tmp;
  89.         int pivot = arr[(left + right) / 2];
  90.         while (i <= j) {
  91.             while (arr[i] < pivot)
  92.                 i++;
  93.             while (arr[j] > pivot)
  94.                 j--;
  95.             if (i <= j) { // swap array elements
  96.                 tmp = arr[i];
  97.                 arr[i++] = arr[j];
  98.                 arr[j--] = tmp;
  99.                     } // end if
  100.             }; // end while
  101.         return i;
  102.     } // end partition()
  103.  
  104.     public static void quickSort(int[] arr, int left, int right) {
  105.         int index = partition(arr, left, right);
  106.         if (left < index - 1)
  107.             quickSort(arr, left, index - 1);
  108.         if (index < right)
  109.             quickSort(arr, index, right);
  110.     } // end quickSort(int[], int, int)
  111.  
  112.     public static void quickSort() {
  113.         // this method provided for convenience
  114.         quickSort(theArray, 0, MAX-1);
  115.     } // end quickSort(int[]);
  116.  
  117.     public static void bubbleSort() {
  118.         // apply bubblesort algorithm to array theArray
  119.         int i,j,temp;
  120.         int len = theArray.length;
  121.         boolean swaps; // optimise code: when no swaps, array is sorted
  122.      
  123.         for (i=0; i<len-1; i++) {
  124.             swaps = false;
  125.             for (j=0; j<len-1-i; j++) {
  126.                 if (theArray[j]>theArray[j+1]) {
  127.                     // swap
  128.                     temp=theArray[j];
  129.                     theArray[j]=theArray[j+1];
  130.                     theArray[j+1]=temp;
  131.                     swaps = true;
  132.                 }//end if
  133.             } //end for j
  134.             if (!swaps) break; // from outer loop
  135.         } //end for i
  136.     } //end bubbleSort()
  137.  
  138. } // end class TestQuicksort
  139.  
  140. class NanoTimer {
  141.  
  142.     /**
  143.     *   Class:      NanoTimer.java
  144.     *   Purpose:    Get program running times in nanoseconds (billionths of a second)
  145.     *   Creator:    Chris Clarke
  146.     *   Created:    03.04.2014
  147.     */
  148.  
  149.     private long startTime = 0, runningTime = -1;
  150.  
  151.     public void setStartTime() {
  152.         startTime = System.nanoTime();
  153.     } // end setStartTime()
  154.  
  155.     public void setStopTime() {
  156.         runningTime = System.nanoTime() - startTime;
  157.     } // end setStopTime()
  158.  
  159.     public String toString() {
  160.         if (runningTime == -1)
  161.             return "Error in running time.";
  162.  
  163.         return runningTime+" nanoseconds";
  164.     } // end toString()
  165.  
  166. } // end class NanoTimer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement