package timingBase; /** * This class allows any sort which implements it to be passed to the SortTimer * Code which is common to all the sorts is here, instead of having a copy in each sort. * @author levenick */ public abstract class AbstractSort { int[] list; int n; private boolean tooSlow; public abstract void sort(); // so the SortTimeer can say theSort.sort(); any class that extends AbstractSort must implement this public abstract String getName(); // so a sort can return its name for the column header void init(int n) { list = new int[n]; this.n = n; fillItUp(n); } /** * Fills the array with random numbers between 0 and n-1 (inclusive) * @param n */ public void fillItUp(int n) { for (int i = 0; i < n; i++) { list[i] = rand(n); } } int rand(int max) { return (int) (Math.random() * max); } boolean ooo(int i, int j) { return list[i] > list[j]; } /** * Out Of Order - for the array * @return if any pair of elements is out of order */ boolean ooo() { for (int i = 0; i < n - 1; i++) { if (ooo(i, i+1)) { return true; // guilty } } return false; } /** * Swaps the ith and jth elements of the list * @param i - i * @param j - j */ void swap(int i, int j) { int pocket = list[i]; list[i] = list[j]; list[j] = pocket; } void setTooSlow(boolean b) { tooSlow = b; } boolean getTooSlow() { return tooSlow; } }