排序功能 - 如何改进

时间:2011-03-14 19:05:07

标签: java sorting

我有以下代码进行排序。这可以改善吗?

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();

    }

  }

3 个答案:

答案 0 :(得分:3)

看看Collections.sort(列表,比较器) http://download.oracle.com/javase/6/docs/api/java/util/Collections.html

答案 1 :(得分: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());
      }
    }
}

答案 2 :(得分:0)

这里真正的答案与iluxa的相似:你想在Church对象上实现一个Comparator接口(示例代码here,尽管你想要决定什么构成大于/小于教会...),然后你可以使用Collections.sort()对它们进行排序。那将在一天结束时完成工作。

当然,您刚刚在 Stack Overflow 上询问有关排序的建议,因此我不得不问您是否需要就地排序,是什么类型的您正在寻找的Big-O性能,然后要求您在Quicksort,IntroSort,HeapSort,MergeSort和StoogeSort之间进行选择,以便最适合您。

对于踢,我曾经用Java编写了几种:

  • This one迫使Quicksort进入二次时间,这比我原先假设的更难,
  • 这个展示了如何实施MergeSort
  • ,此演示了HeapSort

我为自己的享受和教育做了这些。作为一般规则,您希望坚持使用这些类型的标准库。

相关问题