import java.util.ArrayList; import java.util.Comparator; import java.util.List; import sort.InsertionSort; import sort.QuickSort; import sort.Sort; import sort.SortableComparableData; import sort.SortableDataWithComparator; import sort.SortableDataWithStatistics; import sort.SortableDataWithTracer; import sort.SwapableArray; import sort.SortableData; import sort.SwapableList; /* * Created on 24 oct. 2004 * */ /** * @author baudon * */ public class TestSort { public static void sortAndPrint(SortableData sd, Sort sort) { sort.doSort(sd); for (int i = 0; i < sd.size(); ++i) { System.out.print(sd.get(i) + " "); } System.out.println(); } public static void initList(List l, T[] a) { l.clear(); for (T t : a) { l.add(t); } } public static void main(String[] args) { String[] args2 = new String[args.length]; System.arraycopy(args, 0, args2, 0, args.length); Sort isort = new InsertionSort(); Sort qsort = new QuickSort(); List ls = new ArrayList(); sortAndPrint(new SortableComparableData( new SwapableArray(args2)), isort); System.arraycopy(args, 0, args2, 0, args.length); sortAndPrint(new SortableComparableData( new SwapableArray(args2)), qsort); initList(ls, args); sortAndPrint(new SortableComparableData( new SwapableList(ls)), isort); initList(ls, args); for (String s : ls) { System.out.println(s + " " + s.hashCode()); } sortAndPrint(new SortableDataWithComparator( new SwapableList(ls), new Comparator() { public int compare(Object o1, Object o2) { int h1 = o1.hashCode(); int h2 = o2.hashCode(); return h1 < h2 ? -1 : (h1 == h2 ? 0 : 1); } } ), qsort); initList(ls, args); sortAndPrint(new SortableDataWithComparator( new SwapableList(ls), new Comparator() { public int compare(String s1, String s2) { return -s1.compareTo(s2); } }), qsort); initList(ls, args); SortableDataWithStatistics sdws = new SortableDataWithStatistics( new SortableDataWithTracer(new SortableComparableData(new SwapableList(ls)))); sortAndPrint(sdws, qsort); System.out.println("Stats: " + sdws.getSwapStat() + " swap, " + sdws.getGetStat() + " get, " + sdws.getCompareStat() + " compare"); } }