SHARE
TWEET

Untitled

a guest Mar 26th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Arrays;
  2.  
  3. public class SortPerformanceTester_2TanK {
  4.  
  5.    public static void main( String[] args ) {
  6.       final int SIZE = 100000;
  7.       Quiz[] arr = new Quiz[SIZE]; /// Use your class NOT Integer i.e: Zebra[] arr = new Zebra[SIZE];
  8.       // Making all the objects for the array
  9.       for( int i = 0; i < arr.length; i++ ) {
  10.          arr[i] = new Quiz((int)(Math.random()*100));  // i.e.:  arr[i] = new Zebra( Math.random() * 10000 );
  11.       }
  12.      
  13.       // Test using arrays' sort method
  14.       long start = System.currentTimeMillis();
  15.       Arrays.sort( arr ); // this works if arr is an array of objects that implement Comparable
  16.       long stop = System.currentTimeMillis();
  17.       System.out.println("It took " + (stop - start) + "ms to sort " + SIZE + " records.");
  18.      
  19.       // Repopulate the array to test a 2nd time:
  20.       for( int i = 0; i < arr.length; i++ ) {
  21.          arr[i] = new Quiz((int)(Math.random()*100)); // i.e:   arr[i] = new Zebra( Math.random() * 10000 );
  22.       }  
  23.       // Test using your Selection Sort method:
  24.       /*
  25.       for( int i = 0; i < arr.length; i++ ) {
  26.          System.out.printf("%6d ", arr[i]);
  27.          if( i%10 == 0 ) System.out.println();
  28.       }
  29.       */
  30.       start = System.currentTimeMillis();
  31.       organize(arr);
  32.       stop = System.currentTimeMillis();
  33.       System.out.println("It took " + (stop - start) + "ms to sort " + SIZE + " records.");
  34.      
  35.       /*
  36.       for (int i = 0; i < arr.length; i++)
  37.       {
  38.          System.out.println( arr[i]);
  39.       }
  40.       */
  41.  
  42.    }
  43.    
  44.    // Copy your selection sort method (maybe called "organize") below
  45.    //  Make it static and have it use arrays instead of ArrayList
  46.    
  47.    public static void organize(Comparable theArr[])
  48.    {
  49.       int n = theArr.length;
  50.       for (int i = 0; i < n-1; i++)
  51.       {
  52.          int maxIndex = i;
  53.          for (int j = i+1; j < n; j++)
  54.          {
  55.             if (theArr[j].compareTo( theArr[maxIndex] ) > 0)
  56.                maxIndex = j;
  57.          }
  58.      
  59.          Comparable temp = theArr[maxIndex];
  60.          theArr[maxIndex] = theArr[i];
  61.          theArr[i] = temp;
  62.       }
  63.    }
  64.        
  65. }
  66.  
  67. // Copy your chosen class below ... have it implement Comparable
  68.  
  69. class Quiz implements Comparable
  70. {
  71.    private int numScore;
  72.    private String letter;
  73.    
  74.    public Quiz (int theScore)
  75.    {
  76.       numScore = theScore;
  77.       letter = "";
  78.       if (numScore >= 90)
  79.          letter = "A";
  80.       if ((numScore >= 80) && (numScore < 90))
  81.          letter = "B";
  82.       if ((numScore >= 70) && (numScore < 80))
  83.          letter = "C";
  84.       if ((numScore >= 60) && (numScore < 70))
  85.          letter = "D";
  86.       if (numScore < 60)
  87.          letter = "F";
  88.    }
  89.    
  90.    public String theLetter()
  91.    {
  92.       //System.out.println(letter);
  93.       return letter;
  94.    }
  95.    
  96.    public double getMeasure()
  97.    {
  98.       return numScore;
  99.    }
  100.    
  101.    public String toString()
  102.    {
  103.       return "Quiz score: " + numScore + ", letter grade: " + letter;
  104.    }
  105.    
  106.    public int compareTo (Object that)
  107.    {
  108.       Quiz otherQuiz = (Quiz) that;
  109.       int rank1 = (int) (this.numScore * 100);
  110.       int rank2 = (int) (otherQuiz.numScore * 100);
  111.      
  112.       int result = rank1 - rank2;
  113.       return result;
  114.    }
  115. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top