SE450: Algorithm Animation [34/36] Previous pageContentsNext page

file:horstmann/ch09_animation1/Sorter.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package horstmann.ch09_animation1;
import java.util.Comparator;

/**
   This runnable executes a sort algorithm.
   When two elements are compared, the algorithm
   pauses and updates a panel.
 */
public class Sorter implements Runnable
{
  /**
      Constructs the sorter.
      @param values the array to sort
      @param panel the panel for displaying the array
   */
  public Sorter(Double[] values, ArrayComponent panel)
  {
    this.values = values;
    this.panel = panel;
  }

  public void run()
  {
    Comparator<Double> comp = (d1, d2) -> {
      panel.setValues(values, d1, d2);
      try
      {
        Thread.sleep(DELAY);
      }
      catch (InterruptedException exception)
      {
        Thread.currentThread().interrupt();
      }
      return (d1).compareTo(d2);
    };
    MergeSorter.sort(values, comp);
    panel.setValues(values, null, null);
  }

  private Double[] values;
  private ArrayComponent panel;
  private static final int DELAY = 100;
}

Previous pageContentsNext page