001 package lrs.visitor; 002 import lrs.*; 003 004 // This visitor implements a selection sort on a LRS containing 005 // Integer data. Always returns the whole list. 006 public class SelectionSort implements IAlgo{ 007 008 AGetExtrema aGetExtrema; 009 010 public SelectionSort(AGetExtrema getExtrema) { 011 aGetExtrema = getExtrema; 012 } 013 014 015 public Object emptyCase(LRStruct host, Object... param) { 016 return host; // Sort is done! 017 } 018 019 public Object nonEmptyCase(LRStruct host, Object... param) { 020 Integer min = (Integer) host.execute( aGetExtrema, param); // Get the min value of the list 021 host.execute(RemoveItem.Singleton, min); // Remove that min value from the list 022 host.insertFront(min); // Insert that min value at the front of the list 023 host.getRest().execute(this, param); // Recurse on the rest of the list 024 return host; // Always return the whole list. 025 } 026 }