package com.liuxt.sort;import java.io.IOException;import java.io.InputStream;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Properties;
public class SortTemplate {
private Map<String,Sort> sortMap=new HashMap<String,Sort>();
private int maxElement = 100000; private int dataLength=5000;
private Properties sortProperties = new Properties(); /** * 构造一个模版类 * @throws Exception */ public SortTemplate() throws Exception{ this.loadSortAlgorithm(); } /** * 从当前的类路径下获取文本文件,把配置内容独到该类的Map结构中。 * @throws Exception */ private void loadSortAlgorithm() throws Exception {
InputStream inputStrem = this.getClass().getClassLoader() .getResourceAsStream("com/liuxt/sort/sort.properties"); if (inputStrem != null) { try { sortProperties.load(inputStrem); //System.out.println(sortProperties); } catch (IOException e) { e.printStackTrace(); } } String sortName=""; for(int i=0;i<SortUtil.sortNames.length;i++){ sortName=SortUtil.sortNames[i]; this.sortMap.put(sortName,this.instantiate(sortName)); } } /** * 根据算法取对应的算法子类实例。 * @param name * @return */ public Sort getSortByName(String name){ return this.sortMap.get(name); } /** * 根据算法的名字,实例化对应的子类。 * @param name * @return * @throws Exception */ private Sort instantiate(String name) throws Exception { String className = (String) this.sortProperties.get(name); Sort sort = null; if (className != null) { try { sort = (Sort) Class.forName(className).newInstance(); } catch (Exception e) { //e.printStackTrace(); throw e; } } return sort;
}
/** * 根据算法的名称,去排序。 * @param name * @param data * @throws Exception */ public void sortDataByName(String algorithmName ) throws Exception { Sort sort=this.sortMap.get(algorithmName); if(sort==null){ throw new Exception("选择无效的算法"); } int[] data=SortUtil.createData(this.dataLength,this.maxElement); TimeWatch timeWatch=new TimeWatch(); timeWatch.start(); sort.sortData(data); timeWatch.stop(); //displayData(data); timeWatch.showElapsedTime(); SortUtil.showResult(data); }
/** * 获取排序算法的所有名字。 * @return */ public String[] getSortNames() { String[] names=new String[this.sortMap.size()]; Iterator iterator=this.sortMap.keySet().iterator(); int i=0; while(iterator.hasNext()){ names[i++]=(String)iterator.next(); } return names; }
public int getMaxElement() { return maxElement; }
public void setMaxElement(int maxElement) { this.maxElement = maxElement; }
/** * 用所有的排序算法排序,收集数据 * */ public void sortDataByAll() { String[] sortMethods=this.getSortNames(); int[] tempData; Sort sortInstance=null; for(int i=0;i<sortMethods.length;i++){ tempData=SortUtil.createData(this.dataLength,this.maxElement); sortInstance=this.getSortByName(sortMethods[i]); TimeWatch timeWatch=new TimeWatch(); timeWatch.start(); sortInstance.sortData(tempData); timeWatch.stop(); SortUtil.setTime(sortMethods[i],timeWatch.getTimeInMillis()); } SortUtil.showTimeStatistic(); }
public int getDataLength() { return dataLength; }
public void setDataLength(int dataLength) { this.dataLength = dataLength; }
}
相关资源:函数模板sort<T>,