2011-03-14 27 views
0

我有以下代碼進行排序。這可以改善嗎?排序功能 - 這怎麼可以改進

import java.util.*; 
class Church { 
    private String name; 
    private String pastor; 
    public Church(String name, String pastor) { 
     this.name = name; 
     this.pastor = pastor; 
    } 
    public String getPastor() { 
     return pastor; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setPastor(String pastor) { 
     this.pastor = pastor; 
    } 
    public String toString() { 
     return getName() + " is Pastored by "+getPastor(); 
    } 
    public int compareByPastor(Church c) { 
     int x = pastor.compareTo(c.getPastor()); 
     return x; 
    } 
    public int compareByName(Church c) { 
     int x = name.compareTo(c.getName()); 
     return x; 
    } 
} 

class Churches { 
    private final List<Church> churches; 

    public Churches() { 
     churches = new ArrayList<Church>(); 
    } 
    public void addWithoutSorting(Church c) { 
     churches.add(c); 
    } 

    //You could always add using this method 
    public void addWithSorting(Church c) { 

    } 
    public void display() { 
     for(int j = 0; j < churches.size(); j++) { 
      System.out.print(churches.get(j).toString()); 
      System.out.println(""); 
     } 
    } 
    public List<Church> getChurches() { 
     return churches; 
    } 
    public void sortBy(String s) { 
     for (int i = 1; i < churches.size(); i++) { 
      int j; 
      Church val = churches.get(i); 
      for (j = i-1; j > -1; j--) { 
       Church temp = churches.get(j); 
       if(s.equals("Pastor")) { 
        if (temp.compareByPastor(val) <= 0) { 
         break; 
        } 
       } 
       else if(s.equals("Name")) { 
        if (temp.compareByName(val) <= 0) { 
          break; 
        } 
       } 
       churches.set(j+1, temp); 
      } 
      churches.set(j+1, val); 
     } 
    } 

    public static void main(String[] args) { 
     Churches baptists = new Churches(); 
     baptists.addWithoutSorting(new Church("Pac", "Pastor G")); 
     baptists.addWithoutSorting(new Church("New Life", "Tudor")); 
     baptists.addWithoutSorting(new Church("My Church", "r035198x")); 
     baptists.addWithoutSorting(new Church("AFM", "Cathy")); 
     System.out.println("**********************Before Sorting***********************"); 
     baptists.display(); 
     baptists.sortBy("Pastor"); 
     System.out.println("**********************After sorting by Pastor**************"); 
     baptists.display(); 
     baptists.sortBy("Name"); 
     System.out.println("**********************After sorting by Name****************"); 
     baptists.display(); 

    } 

    } 
+0

從這個問題來看,你不清楚你想如何分類教堂:按姓名,按牧師的名字?按會衆的大小?通過尖頂高度? – rtperson

回答

3

在Collections.sort(名單時,比較器) http://download.oracle.com/javase/6/docs/api/java/util/Collections.html

+2

編寫兩個自定義比較器:一個按牧師排序,另一個按名稱排序。當你想由牧師排序時,調用'Collections.sort(yourList,pastorComparator)'。當你想按名稱排序時,調用'Collections.sort(yourList,nameComparator)'。 – mre

0
class Churches 
{ 
    public void sortBy(String attribute) 
    { 
     Comparator<Church> c = null; 

     if ("Name".equals(attribute)) c = new ChurchNameComparator(); 
     else if ("Pastor".equals(attribute)) c = new ChurchNameComparator(); 
     else System.out.println("unexpected sort attribute : '" + attribute + "'"); 

     if (c != null) Collections.sort(churches, c); 
    } 

    private static final class ChurchNameComparator implements Comparator<Church> 
    { 
     public int compare(Church c1, Church c2) 
     { 
     return c1.getName().compareTo(c2.getName()); 
     } 
    } 

    private static final class ChurchPastorComparator implements Comparator<Church> 
    { 
     public int compare(Church c1, Church c2) 
     { 
     return c1.getPastor().compareTo(c2.getPastor()); 
     } 
    } 
} 
0

這裏真正的答案看看是線幾乎與iluxa的:你要實現你的教會比較接口對象(示例代碼here,雖然你會想要決定什麼構成比教堂更大/更小......),然後你可以使用Collections.sort()對它們進行排序。這將在一天結束時完成工作。

當然,你剛纔問的意見有關堆棧溢出排序,所以我覺得有必要問你是否需要一個就地排序,你要找什麼樣的大O性能,然後要求您在Quicksort,IntroSort,HeapSort,MergeSort和StoogeSort之間進行選擇,以便最適合您的項目。

踢,我曾經編寫了幾個種類在Java中:

  • This one軍隊快速排序到二次的時間,這是很難做的比我原來設想,
  • 這一個展示如何實施MergeSort
  • 而這一次演示了HeapSort

我做這些我自己的享受和教育。作爲一般規則,您希望堅持使用標準庫來處理這些事情。