2015-04-21 59 views
6

我有一個叫做apple的類,它包含3個值,分別爲int x,int yint weight。然後我創建了一個蘋果類型對象的數組。現在我想根據重量排序對象數組,這意味着具有最低權重的蘋果對象應該是第一個等等。在java中排序對象數組的最快方法

我知道有很多方法可以通過使用Arrays.sort等或比較器來實現。

我想知道在Java中做這種排序的最快方法是什麼?可能有一個案件,我有500,000個對象,所以我想知道我應該使用哪種類型,更重要的是哪種方法會給我最好的方法。我甚至用Hoare分區寫了我自己的快速排序。

代碼爲蘋果類

public class Apple { 
    public int x; 
    public int y; 
    public int weight; 

    public Apple(int a, int b, int w) { 
     x = a; 
     y = b; 
     weight = w; 
    } 
} 

代碼主類

public class main { 
    static Apple[] appleArray; 

    public static void main(String[] args) { 
     Scanner sc = new Scanner(System.in); 
     int size = sc.nextInt(); 
     int totalApples = sc.nextInt(); 
     appleArray = new Edge[totalApples]; 
     int x = 10; 
     int y = 20; 
     int w = 30; 

     for(int i = 0; i < size; i ++){ 
      appleArray[i] = new Apple(x,y,w); 
      x++; 
      y++; 
      w++; 
     } 

     //Now i want to sort array of apple objects based on weight 
    } 
} 
+1

如果你想要最快的排序,你應該知道不同的算法速度取決於數據的性質和分佈。我相信「寫和衡量績效」是你的問題的唯一正確答案。 Collections.sort很可能會做得很好。 –

+0

我懷疑你只給出這個代碼作爲例子,但是對這個特定的主要方法按權重排序的最快方法是什麼都不做 - 權重已經隨着數組索引而增加。 –

+0

是的,它只是一個例子,他們可能是隨機@AndyTurner,雖然 – user1010101

回答

6

這本書來決定適合您需求的最佳排序有用的小抄:https://www.safaribooksonline.com/library/view/algorithms-in-a/9780596516246/ch04s09.html

Arrays.sort命令使用quicksort implementation,這是適用於許多情況下,最簡單的解決方案。爲了您的示例代碼,這可能是:

Arrays.sort(appleArray, new Comparator<Apple>(){ 
    @Override 
    public int compare(Apple apple1, Apple apple2){ 
     return apple1.weight - apple2.weight; 
    } 
}); 

最快的解決方案

你的情況,你有一個包含重複一大陣,例如50000個蘋果陣列中所有可能重3盎司...因此,您可能會選擇實施bucket sort以通過快速排序來提高性能,在這種情況下可能會造成浪費。這是一個example implementation

也許基準幾個研究選擇,使用Java API,當它適合,以確定您的輸入集的最佳解決方案。

+0

是否有可能做出什麼樣的改變,我試圖做一個compareTo方法,並使用Arrays.sort它不會工作。如果你能用某種東西編輯這個答案,我將不勝感激。 – user1010101

+0

如果你可以在你的問題中提供一些代碼,我可能會在我的答案中更具體。 – seanhodges

+0

我已經添加了一個示例'bucket sort'實現的鏈接,對於使用Arrays.sort()的示例,這裏的答案可能有幫助嗎? http://stackoverflow.com/questions/14863433/sorting-array-of-type-object-using-arrays-sort-method – seanhodges

3

可以使用Arrays.sort通過自定義Comparator或定義你AppleComparable

+1

很好的觀察,所以它會是這樣的 - > Arrays.sort(appleArray,appleArray.weight)?感謝您的回答,如果您能證明您將如何做到這一點,那就太好了。 – user1010101

7

我會先用Java API。如果這個速度不夠快,那麼我會搜索一個優化的排序庫。

也考慮一個數據庫,數據庫引擎是快速和優化的大型數據集排序。

2

我們可以使用Collections.sort和自定義比較器。

class Apple { 
    public final int weight; 
    // ... 
}; 

List<Apple> apples = // ... 

Collections.sort(apples, new Comparator<Apple>() { 
    @Override public int compare(Apple a1, Apple a2) { 
     return a1.weight - a2.weight; // Ascending 
    } 

}); 
+0

爲什麼這是最快的? –

2

如果你的對象有過,你必須進行排序,你可以不喜歡這個 -

List<Object> obj = new ArrayList<Object>(); 

Collections.sort(obj, new Comparator<Object>() { 
    @Override 
    public int compare(Object object1, Object object2) { 
     return object1.getNumber() > object2.getNumber() ? 1 : -1; 
    } 
}); 

如果沒有數字或整型過,你可以對它進行排序的任何數字或整型你只需要在你的對象中有字符串,而不是通過枚舉賦值給字符串。

enum Code { 
    Str1(1), Str2(2), Str3(3), Str4(4), Str5(5)); 

    int sortNumber; 

    Code(int sortNumber) { 
     this.sortNumber = sortNumber; 
    } 

    int returnNumber() { 
     return sortNumber; 
    } 
}; 

public static void main(String[] args) { 
    List<Object> obj = new ArrayList<Object>(); 

    Collections.sort(obj, new Comparator<Object>() { 
     @Override 
     public int compare(Object object1, Object object2) { 
      return Code.valueOf(object1.getStr()).returnNumber() > Code.valueOf(object2.getStr()).returnNumber() ? 1 : -1; 
     } 
    }); 
}