01: import java.util.*;
02:
03: /**
04: This runnable executes a sort algorithm.
05: When two elements are compared, the algorithm
06: pauses and updates a panel.
07: */
08: public class Sorter implements Runnable
09: {
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(Object[] values, ArrayPanel panel)
16: {
17: this.values = values;
18: this.panel = panel;
19: }
20:
21: public void run()
22: {
23: Comparator comp = new
24: Comparator()
25: {
26: public int compare(Object o1, Object o2)
27: {
28: panel.setValues(values, o1, o2);
29: try
30: {
31: Thread.sleep(DELAY);
32: }
33: catch (InterruptedException exception)
34: {
35: Thread.currentThread().interrupt();
36: }
37: return ((Integer) o1).compareTo(o2);
38: }
39: };
40: MergeSorter.sort(values, comp);
41: panel.setValues(values, null, null);
42: }
43:
44: private Object[] values;
45: private ArrayPanel panel;
46: private static final int DELAY = 100;
47: }