Selection Sort

This java program gives a visual representation of how selection sort works.

The Selection Sort Program


A guided tour of Sorting.java

The basic structure is:


class Sorting extends Applet implements Runnable {

    // declare some buttons
    // declare the constants SWAPPING=1 and SWAPPED=2
    // declare the global data structures table and status.

    public void init() {
       // layout the Applet's panel and validate (show) it
    }

    void initialize( int [] table, int [] status ) {
       // fill in random data to table for the sort
    }

    public boolean action ( Event e, Object a ) {
       // handle the "sortButton" event
    }

    boolean sortOneStep( int [] table, int [] status ) {
       // (1) Look at status and determine if this is a swapping step
       //     or a find-min step
       // (2) If a swapping step, swap the two locations marked
       //     SWAPPING in status, and mark then SWAPPED
       // (3) If a find-min step, find the min and mark it SWAPPING
    }

    public void run() {
       // the thread which does the algorithm animation.
       // while sortOneStep returns true, 
       //    (1) redraw the screen
       //    (2) sleep a little bit
    }

}

class SortingView extends Panel {

     SortingView( int [] table, int [] status ) {
        // constructor method, remember table and status
     }
  
     public void paint( Graphics gc ) {
        // this is how you repaint the SortingView,
        // put a square of color, according to status[i] in
        // location (i,table[i]). (not exactly, both i and
        // table[i] are scaled and translated to fit.
     } 
}