package lrs.visitor; import lrs.*; /** * Sorts by splitting the host into two sublists, sorts the sublists, then * merges the sorted sublists. */ public class MergeSorter1 implements IAlgo { public static final MergeSorter1 Singleton = new MergeSorter1 (); private MergeSorter1() { } /** * @param host * @param input not used. * @return null */ public Object nullCase(LRStruct host, Object input) { return null; // there is nothing to sort } /** * @param host * @param input not used. * @return null. */ public Object nonNullCase(final LRStruct host, Object input) { return host.getRest().execute ( new IAlgo () { // Case host has one element (i.e. hostTail is empty). public Object nullCase (LRStruct hostTail, Object inputNotUsed) { return null; } public Object nonNullCase (LRStruct hostTail, Object inputNotUsed) { LRStruct sublist = (LRStruct)host.execute (EveryOther1.Singleton, null); host.execute (MergeSorter1.Singleton, null); sublist.execute (MergeSorter1.Singleton, null); host.execute (Merger.Singleton, sublist); return null; } } , null); } public static void main(String[] args) { LRStruct l1 = new LRStruct (); System.out.println ("l1: " + l1); System.out.println ("Merge sort l1..."); l1.execute(MergeSorter1.Singleton, null); System.out.println ("l1: " + l1); l1.insertFront (new Integer (-9)); l1.insertFront (new Integer (15)); l1.insertFront (new Integer (263)); l1.insertFront (new Integer (-72)); l1.insertFront (new Integer (0)); l1.insertFront (new Integer (48)); /* */ System.out.println ("l1: " + l1); System.out.println ("Merge sort l1..."); l1.execute(MergeSorter1.Singleton, null); System.out.println ("l1: " + l1); } }