The fastest way to sort an array of objects in java

I have a class called apple that contains 3 values ​​like int x , int y and int weight . Then I created an array of objects like Apple. Now I want to sort the array of objects by weight, meaning that the object with the smallest weight should be the first, etc.

I know that there are many ways to achieve this using Array.sort, etc. or comparators.

I was wondering what is the fastest way to make this look in Java? There may be times when I have 500,000 objects, so I want to know what type I should use, more importantly, which approach will give me the best approach. I even wrote my own quick view with the Hoare section.

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 the 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 } } 
+6
source share
5 answers

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

The easiest solution

The Arrays.sort team uses a quicksort implementation that is suitable for many situations. For your sample code, this could be:

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

Fastest solution

In your case, you have a large array containing repetitions, for example, 50,000 apples in your array can weigh 3 ounces ... Therefore, you can choose the sorting of the bucket to improve performance compared to quick sorting, which can be wasteful in such conditions . The following is an example.

Perhaps compare some of the options explored using the Java API when appropriate to determine the best solution for your input set.

+6
source

I would use the Java API first . If this is not fast enough, I would look for an optimized sorting library.

Also consider a database, database engines are fast and optimized for sorting large data sets.

+7
source

You can use Arrays.sort pass a custom Comparator or define Apple as Comparable

+3
source

We can use Collections.sort with a custom Comparator.

 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 } }); 
+2
source

If your object has a Number or Integer that you should sort by, you can do it like this:

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

And if there is no Number or Integer by which you can sort it, and you just have lines in your object, than assign the value of Strings to an enumeration.

 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; } }); } 
+2
source

Source: https://habr.com/ru/post/985623/


All Articles