// Tile.java ===================================================== class Tile extends Rectangle implements Sortable { public Tile(int x, int y, int w, int h, int zz) { super(x, y, w, h); z = zz; } // constructor public int compare(Sortable b) { Tile tb = (Tile)b; return z - tb.z; } // compare public String toString() { return super.toString() + " [z=" + z + "]"; } // toString private int z; } // Tile // TileTest.java ===================================================== import java.awt.*; public class TileTest { public static void main(String[] args) { Tile[] a = new Tile[20]; int i; for (i = 0; i < a.length; i ++) a[i] = new Tile(i, i, 10, 20, (int)(100*Math.random())); SecondSort.sort(a); for (i = 0; i < a.length; i ++) System.out.println(a[i]); } // main } // TileTest // SecondSort.java ===================================================== import java.util.*; public class SecondSort { public static void sort(Sortable[] a) { int k; int n = a.length; k = 0; while (k < n) { int j = getSmallest(a, k); exchange(a, k, j); k ++; } // while } // Sort static void exchange(Sortable[] a, int k, int j) { Sortable temp; temp = a[k]; a[k] = a[j]; a[j] = temp; } // exchange static int getSmallest(Sortable[] a, int k) { Sortable s = a[k]; int n = a.length; int minLoc = k; for (int i = k; i < n; i ++) if (s.compare(a[i]) > 0) // s > a[i] { s = a[i]; minLoc = i; } return minLoc; } // getSmallest } // SecondSort // Sortable.java ===================================================== public interface Sortable { public abstract int compare(Sortable b); // child class will implement it }