模板模式以及java算法的復習
針對常用的算法進行重新復習了一下,結合對象數組排序,使用模板模式封裝了一部分,各個子類實現自己的對象數組排序算法。里面稍微涉及到了適配器、策略模式等。使用了泛型,嗯,會帶來一部分代碼的復用。
定義一個算法抽象基類:
一個冒泡算法實現:
一個插入排序:
其它兩個,快速,選擇排序不再一一粘貼。
附加一個客戶端測試類:
代碼打包下載地址: 下載
定義一個算法抽象基類:
/**
* 對象數組排序算法,藉此復習幾種常見排序算法
*
* 模板模式
*
**/
public abstract class Sort {
/**
* 對象數組排序
* @param <T>
* @param ts 傳入要排序的對象數組
* @param c 需要傳入的自定義比較器
*/
public abstract <T> void sort(T[] ts, Comparator<? super T> c);
/**
* 對象數組排序
* @param <T>
* @param datas 傳入的對象必須已經實現了Comparable接口
*/
public <T extends Comparable<T>> void sort(T[] datas) {
sort(datas, new ComparatorAdapter<T>());
}
/**
* 定義一個比較適配器內部類,使Comparable接口適配成Comparator接口
*
* @author xiaomin
*
* @param <T>
*/
private class ComparatorAdapter<T extends Comparable<T>> implements
Comparator<T> {
public int compare(T o1, T o2) {
return o1.compareTo(o2);
}
}
/**
* 交換對象數組里面的兩個元素位置
* @param <T>
* @param ints
* @param index1
* @param index2
*/
protected <T> void swap(T[] ints, int index1, int index2) {
T temp = ints[index1];
ints[index1] = ints[index2];
ints[index2] = temp;
}
}
/**
* 冒泡排序----交換排序的一種
* 方法:相鄰兩元素進行比較,如有需要則進行交換,每完成一次循環就將最大元素排在最后(如從小到大排序),下一次循環是將其他的數進行類似操作。
* 性能:比較次數O(n^2),n^2/2;交換次數O(n^2),n^2/4
*
**/
public class Bubble extends Sort{
@Override
public <T> void sort(T[] datas, Comparator<? super T> c){
for(int i = (datas.length - 1); i > 0; i--){
for(int j = 0; j < i; j++){
if(c.compare(datas[j], datas[j + 1]) > 0){
super.swap(datas, j, j+1);
}
}
}
}
public static void main(String ... args){
Bubble b = new Bubble();
Integer [] ints = {1, 23, 0, 2, 356, 367, 6,-1, 256};
b.sort(ints);
System.out.println(Arrays.toString(ints));
}
}
/**
* 插入排序
* 方法:將一個記錄插入到已排好序的有序表(有可能是空表)中,從而得到一個新的記錄數增1的有序表。
* 性能:比較次數O(n^2),n^2/4
* 復制次數O(n),n^2/4
* 比較次數是前兩者的一般,而復制所需的CPU時間較交換少,所以性能上比冒泡排序提高一倍多,而比選擇排序也要快。
*
*/
public class Insert extends Sort{
@Override
public <T> void sort(T[] datas, Comparator<? super T> c){
for(int i = 0; i < datas.length; i++){
for(int j = i; j > 0; j--){
if(c.compare(datas[j -1],datas[j]) > 0){
super.swap(datas, j-1, j);
}
}
}
}
public static void main(String [] args){
Insert insert = new Insert();
Double [] doubles = {1.0, 0.3, 3.4, -0.3, 3.0,3.0, 0.34};
insert.sort(doubles);
System.out.println(Arrays.toString(doubles));
}
}
附加一個客戶端測試類:
public class Client {
public static void main(String[] args) {
Person[] persons = { new Person("a", 12), new Person("b", 10),
new Person("demo", 23), new Person("hello", 22),
new Person("hello", 32), new Person("xiaomeng", 2) };
Person [] selectPersons = persons.clone();
Person [] quickPersons = persons.clone();
Person [] quickCustomPersons = persons.clone();
Person [] insertPersons = persons.clone();
System.out.println("排序前 ......");
System.out.println(Arrays.toString(persons));
System.out.println();
System.out.println("冒泡排序后 ......");
new Bubble().sort(persons);
System.out.println(Arrays.toString(persons));
System.out.println();
System.out.println("選擇排序后 ......");
new Select().sort(selectPersons);
System.out.println(Arrays.toString(selectPersons));
System.out.println();
System.out.println("插入排序后 ......");
new Insert().sort(insertPersons);
System.out.println(Arrays.toString(insertPersons));
System.out.println();
System.out.println("快速排序后 ......");
new Quick().sort(quickPersons);
System.out.println(Arrays.toString(quickPersons));
System.out.println();
System.out.println("使用快速自定義排序 ......");
new Quick().sort(quickCustomPersons, new Comparator<Person>() {
public int compare(Person o1, Person o2) {// 倒敘排列
return o1.getAge() > o2.getAge() ? -1 : (o1.getAge() == o2.getAge() ? 0 : 1);
}
});
System.out.println(Arrays.toString(quickCustomPersons));
}
}
class Person implements Serializable, Comparable<Person> {
private static final long serialVersionUID = -23536L;
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// 正序排列
public int compareTo(Person o) {
if (o == null)
return -1;
return this.getAge() < o.getAge() ? -1 : (this.getAge() == o.getAge() ? 0 : 1);
}
public String toString() {
return "name : " + getName() + " age : " + getAge();
}
}
posted on 2010-10-06 10:59 nieyong 閱讀(391) 評論(0) 編輯 收藏 所屬分類: Java