0001 /* 0002 * 㷨 0003 */ 0004 0005 package dsa; 0006 0007 public class Sorter_Bubblesort implements Sorter { 0008 private Comparator C; 0009 0010 public Sorter_Bubblesort() 0011 { this(new ComparatorDefault()); } 0012 0013 public Sorter_Bubblesort(Comparator comp) 0014 { C = comp; } 0015 0016 public void sort(Sequence S) { 0017 int n = S.getSize(); 0018 for (int i = 0; i < n; i++) 0019 for (int j = 0; j < n - i - 1; j++) 0020 if (0 < C.compare(S.getAtRank(j), S.getAtRank(j + 1))) { 0021 Object temp = S.getAtRank(j); 0022 S.replaceAtRank(j, S.getAtRank(j + 1)); 0023 S.replaceAtRank(j + 1, temp); 0024 } 0025 } 0026 }