CS 241 Data Structures- Lab 3, Due 2/21

Sort Timing

Your task for this lab is to implement bubble sort, insertion sort, and selection sort, and time them on various sized lists by passing them to a SortTimer.

Requirements

Copy/paste (most of) this code into your Frame and write classes to make it code work (use NetBeans to create the Frame, then copy this piecemeal):
public class TimerFrame extends javax.swing.JFrame {

    public TimerFrame() {  // this constructor creates the form and displays it
        initComponents();
        setSize(1000, 500);
        setTitle("SortTimer Frame");
        setVisible(true);

        emit("\t\t\tT I M I N G   T H R E E   S O R T S !\n\n");
        timeStuff();
        emit("\n\nAll done!");
    }
    
    /** time each of the sorts for length n
     * done by n instead of by sort so the output is easy
     */
    private void timeStuff() {
        for (int i = 0; i < 8; i++) {
            int n = (int) Math.pow(2, i);
            time(n * 1000);  
            emit("\n");
        }
    }

    private void time(int n) {
        // create an array with an instance of each sort of length n 
        AbstractSort[] sorts = {new BubbleSort(n), new InsertSort(n), new SelectionSort(n)};

        for (int i = 0; i < sorts.length; i++) { // for each of the sorts
            AbstractSort aSort = sorts[i];
            long time = SortTimer.time(aSort);
            emit(aSort.getName() + ": n=" + n + " t=" + time);
            if (n < 50) { // for testing whether the sort actually works...
                System.out.println(aSort);
            }
        }
    }

    void emit(String s) {
        theTA.append(s + "\t");
    }
To produce output that looks something like: this

Details

  1. Your AbstractSort class might start out like this. You are not required to write a List class, but however you represent the list it should be declared in AbstractSort, and the list should be initialized when it is instantiated, not while you are timing. Notice the abstract sort method; this requires any class which extends AbstractSort to implement a sort method with this signature (see next link).
  2. Your BubbleSort class might start out like this.
  3. Initialize the lists at random with numbers between 0 and n (where n is the length of the list)
  4. Test your sorts with n=8 or 10, to make sure they actually work! Come up with some way that it is quick to demonstrate that they work when you demo (i.e. a button, or menu item, or test module of whatever stripe you like).

Demo your code during lab for credit.