Source of Sorter.java


  1: import java.util.*;

  3: /**
  4:    This runnable executes a sort algorithm.
  5:    When two elements are compared, the algorithm
  6:    pauses and updates a panel.
  7: */
  8: public class Sorter implements Runnable
  9: {
 10:    /**
 11:       Constructs the sorter.
 12:       @param values the array to sort
 13:       @param panel the panel for displaying the array
 14:    */
 15:    public Sorter(Double[] values, ArrayComponent panel)
 16:    {
 17:       this.values = values;
 18:       this.panel = panel;
 19:    }

 21:    public void run()
 22:    {
 23:       Comparator<Double> comp = new
 24:          Comparator<Double>()
 25:          {
 26:             public int compare(Double d1, Double d2)
 27:             {
 28:                panel.setValues(values, d1, d2);
 29:                try
 30:                {
 31:                   Thread.sleep(DELAY);
 32:                }
 33:                catch (InterruptedException exception)
 34:                {
 35:                   Thread.currentThread().interrupt();
 36:                }
 37:                return (d1).compareTo(d2);
 38:             }
 39:          };
 40:       MergeSorter.sort(values, comp);
 41:       panel.setValues(values, null, null);
 42:    }

 44:    private Double[] values;
 45:    private ArrayComponent panel;
 46:    private static final int DELAY = 100;
 47: }