Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class SortPerformanceTester_2TanK {
- public static void main( String[] args ) {
- final int SIZE = 100000;
- Quiz[] arr = new Quiz[SIZE]; /// Use your class NOT Integer i.e: Zebra[] arr = new Zebra[SIZE];
- // Making all the objects for the array
- for( int i = 0; i < arr.length; i++ ) {
- arr[i] = new Quiz((int)(Math.random()*100)); // i.e.: arr[i] = new Zebra( Math.random() * 10000 );
- }
- // Test using arrays' sort method
- long start = System.currentTimeMillis();
- Arrays.sort( arr ); // this works if arr is an array of objects that implement Comparable
- long stop = System.currentTimeMillis();
- System.out.println("It took " + (stop - start) + "ms to sort " + SIZE + " records.");
- // Repopulate the array to test a 2nd time:
- for( int i = 0; i < arr.length; i++ ) {
- arr[i] = new Quiz((int)(Math.random()*100)); // i.e: arr[i] = new Zebra( Math.random() * 10000 );
- }
- // Test using your Selection Sort method:
- /*
- for( int i = 0; i < arr.length; i++ ) {
- System.out.printf("%6d ", arr[i]);
- if( i%10 == 0 ) System.out.println();
- }
- */
- start = System.currentTimeMillis();
- organize(arr);
- stop = System.currentTimeMillis();
- System.out.println("It took " + (stop - start) + "ms to sort " + SIZE + " records.");
- /*
- for (int i = 0; i < arr.length; i++)
- {
- System.out.println( arr[i]);
- }
- */
- }
- // Copy your selection sort method (maybe called "organize") below
- // Make it static and have it use arrays instead of ArrayList
- public static void organize(Comparable theArr[])
- {
- int n = theArr.length;
- for (int i = 0; i < n-1; i++)
- {
- int maxIndex = i;
- for (int j = i+1; j < n; j++)
- {
- if (theArr[j].compareTo( theArr[maxIndex] ) > 0)
- maxIndex = j;
- }
- Comparable temp = theArr[maxIndex];
- theArr[maxIndex] = theArr[i];
- theArr[i] = temp;
- }
- }
- }
- // Copy your chosen class below ... have it implement Comparable
- class Quiz implements Comparable
- {
- private int numScore;
- private String letter;
- public Quiz (int theScore)
- {
- numScore = theScore;
- letter = "";
- if (numScore >= 90)
- letter = "A";
- if ((numScore >= 80) && (numScore < 90))
- letter = "B";
- if ((numScore >= 70) && (numScore < 80))
- letter = "C";
- if ((numScore >= 60) && (numScore < 70))
- letter = "D";
- if (numScore < 60)
- letter = "F";
- }
- public String theLetter()
- {
- //System.out.println(letter);
- return letter;
- }
- public double getMeasure()
- {
- return numScore;
- }
- public String toString()
- {
- return "Quiz score: " + numScore + ", letter grade: " + letter;
- }
- public int compareTo (Object that)
- {
- Quiz otherQuiz = (Quiz) that;
- int rank1 = (int) (this.numScore * 100);
- int rank2 = (int) (otherQuiz.numScore * 100);
- int result = rank1 - rank2;
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement