// Visualization of sorting algorithms; JDK 1.1 // sestoft@dina.kvl.dk 1997-05-08, 1998-08-23 import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class Visualsort extends Applet { public static void main(String[] args) { Visualsort vs = new Visualsort(); SortFrame sf = new SortFrame(); vs.initGUI(sf); sf.pack(); sf.show(); } public void init() { initGUI(this); } private void initGUI(Container c) { final int size = 150; SortCanvas canvas = new SortCanvas(size); Sorter[] sorters = { new Selectionsort(canvas), new Bubblesort(canvas), new Quicksort(canvas), new Heapsort(canvas)}; SortControls controls = new SortControls(sorters); c.add(canvas); c.add(controls); } } class SortFrame extends Frame { public SortFrame() { super("Some sorting algorithms"); setLayout(new FlowLayout()); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); } } // A SortCanvas is created with a reference to an array of integers. // It has three methods: (1) show two-sided comparison, (2) show // one-sided comparison, (3) show element swap. These are called with // the index of the array which is affected by the operation. // The idea is to show the comparisons by vertical blue lines through // the elements being compared, and to show the swaps just by // redrawing the pictures after the actual swap has been made in the // arrays. // Assumption: at any time the array contains a permutation of // the set { 0, ..., arr.length-1 } class SortCanvas extends Canvas { private final int unit = 2; private int size, compare1, compare2; private int[] arr; // For double buffering private Image buffer; private Graphics buffer_g; public SortCanvas(int size) { this.size = size; setSize(size*unit, size*unit); compare1 = compare2 = -1; } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { if (buffer == null) { buffer = createImage(size*unit, size*unit); buffer_g = buffer.getGraphics(); } buffer_g.setColor(Color.white); buffer_g.fillRect(0, 0, size*unit, size*unit); buffer_g.setColor(Color.black); if (arr != null) { for (int i = 0; i < size; i++) buffer_g.fillRect(i*unit, (size-1-arr[i])*unit, unit, unit); buffer_g.setColor(Color.blue.brighter()); if (compare1 >= 0) buffer_g.fillRect(compare1*unit+(unit-1)/2, 0, 1, size*unit); if (compare2 >= 0) buffer_g.fillRect(compare2*unit+(unit-1)/2, 0, 1, size*unit); } compare1 = compare2 = -1; g.drawImage(buffer, 0, 0, this); } public void setArray(int[] arr) { compare1 = compare2 = -1; this.arr = arr; } public void showcompare1(int s) { compare1 = s; repaint(); } public void showcompare2(int s, int t) { compare1 = s; compare2 = t; repaint(); } public int getCells() { return size; } } class SortControls extends Panel { Button start = new Button("Start"); Button pause = new Button("Continue"); Choice algorithm = new Choice(); Scrollbar speed = new Scrollbar(Scrollbar.HORIZONTAL); Sorter[] sorters; Sorter sorter; Thread animator; SortControls(Sorter[] sorters) { this.sorters = sorters; setLayout(new BorderLayout()); add(speed, BorderLayout.CENTER); speed.setValues(50, 5, 0, 100); for (int i=0; i arr[j+1]) showswap(arr, j, j+1); } } } public String getName() { return "Bubble sort"; } } class Quicksort extends Sorter { public Quicksort(SortCanvas canvas) { this.canvas = canvas; } private void qsort(int[] arr, int a, int b) { if (a < b) { int i = a, j = b; int x = arr[(i+j) / 2]; do { while (arr[i] < x) { showcompare2(i,j); i++; } while (arr[j] > x) { showcompare2(i,j); j--; } if (i <= j) { showswap(arr, i, j); i++; j--; } } while (i <= j); qsort(arr, a, j); qsort(arr, i, b); } } public void showsort(int[] arr, int n) { qsort(arr, 0, n-1); } public String getName() { return "Quicksort"; } } class Heapsort extends Sorter { public Heapsort(SortCanvas canvas) { this.canvas = canvas; } private void heapify(int[] arr, int i, int k) { int j = 2 * i + 1; if (j <= k) { if (j+1 <= k) { showcompare2(j, j+1); if (arr[j] < arr[j+1]) j++; } if (arr[i] < arr[j]) { showcompare2(i, j); showswap(arr, i, j); heapify(arr, j, k); } } } public void showsort(int[] arr, int n) { for (int m=n/2; m >= 0; m--) heapify(arr, m, n-1); for (int m=n-1; m >= 1; m--) { showswap(arr, 0, m); heapify(arr, 0, m-1); } } public String getName() { return "Heap sort"; } }