排序在Java对象的数组最快的方法 [英] Fastest way to sort an array of objects in java

查看:128
本文介绍了排序在Java对象的数组最快的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一类叫做苹果,其中包含3个值作为 INT X INT是 INT重量。然后,我创建了苹果型对象的数组。现在我想基于重量意味着具有最低重量的苹果对象应该是第一等的对象数组进行排序。

我知道有相当多的方式使用Arrays.sort等或比较器来实现这一目标。

我想知道什么是做这种在Java中的最快的方法?有可能是在那里我得为50万名对象,所以我想知道我应该使用哪种类型的情况下,更重要的是它的做法会给我最好的办法。我甚至写了我自己的快速排序与霍尔分区。

code苹果类

 公共类苹果{ 公众诠释X;
 公众诠释Ÿ;
 公众诠释权重;    公共苹果(INT A,INT B,INT W)
    {
        X = A;
        Y = B:
        重量= W;
    }}

code主类

 公共类主要{
    静态苹果[] appleArray;    公共静态无效的主要(字串[] args){       扫描仪SC =新的扫描仪(System.in);
        INT大小= sc.nextInt();
        INT totalApples = sc.nextInt();
        appleArray =新的边缘[totalApples]
        INT X = 10;
        诠释Y = 20;
        INT W = 30;
         的for(int i = 0; I<大小;我++){
             appleArray [I] =新的苹果(X,Y,W);
             X ++;
             ÿ++;
             W¯¯++;
         }        //现在我想基于重量排序苹果对象数组}


解决方案

这本书来决定适合您需求的最优排序有用的小抄:的https://www.safaribooksonline.com/library/view/algorithms-in-a/9780596516246/ch04s09.html

最简单的解决方案

Arrays.sort 命令使用的快速排序实施时,它适合于许多场合。对于你的榜样code,这可能是:

  Arrays.sort(appleArray,新的比较<苹果>(){
    @覆盖
    公众诠释比较(苹果apple1,苹果apple2){
         返回apple1.weight - apple2.weight;
    }
});

最快的解决方案

在你的情况,你有一个包含重复一个大阵,例如50000苹果阵列中可能都体重3盎司......你可能会因此选择实施的桶排序以改善在一个快速排序,它可以是这样的条件下的浪费的性能。下面是一个示例实现

也许基准数研究的选择,使用Java API它适合的时候,以确定您输入设置的最佳解决方案。

I have a Class called apple which contains 3 values as int x, int y and int weight. Then i created an array of apple type objects. Now i want to sort the the array of objects based on weight meaning the the apple object with the lowest weight should be first and so on.

I know there are quite a few ways to achieve this by using Arrays.sort etc or comparators.

I was wondering what is the fastest way of doing this sort in Java? There can be a case where i have 500,000 objects so i want to know which sort i should use, more importantly which approach will give me best approach. i have even wrote my own quick sort with Hoare partition.

Code for Apple class

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;
    }

}

Code for main class

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

}

解决方案

This book has a useful cheat sheet for deciding the optimal sort for your needs: https://www.safaribooksonline.com/library/view/algorithms-in-a/9780596516246/ch04s09.html

The easiest solution

The Arrays.sort command uses a quicksort implementation, which is suitable for many situations. For your example code this might be:

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

The fastest solution

In your case you have a large array containing repetitions, for example 50,000 apples in your array might all weigh 3 ounces... You might therefore opt to implement a bucket sort to improve performance over a quicksort, which can be wasteful under such conditions. Here is an example implementation.

Perhaps benchmark a few researched choices, using the Java API when it suits, to determine the best solution for your input set.

这篇关于排序在Java对象的数组最快的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆