40 likes | 161 Views
About how to implement algorithms. Fall 2013, Haidong Xue. One P roblem, M ultiple A lgorithms. Given a problem Problem Input: IN Algorithms: Alg1(IN), Alg2( IN ), … , AlgN ( IN ) E.g.: your term project sorting. A good way to implement them: the Strategy Pattern.
E N D
About how to implement algorithms Fall 2013, HaidongXue
One Problem, Multiple Algorithms • Given a problem • Problem Input: IN • Algorithms: • Alg1(IN), Alg2(IN), … , AlgN(IN) • E.g.: • your term project • sorting
A good way to implement them: the Strategy Pattern AbstractAlgorithm solve() ConcreteAlgorithm2 solve() ConcreteAlgorithmN solve() ConcreteAlgorithm1 solve() …
Example: sorting uses Tester AbstractSortingAlgorithm sort() InsertionSort sort() XSort sort() Quicksort() sort() … Let’s do it!