JDK built-in solutions

The built-in solution is named sort() and it comes in many different flavors in the java.util.Arrays class (15+ flavors).

Behind the sort() method, there is a performant sorting algorithm of the Quicksort type, named Dual-Pivot Quicksort.

Let's assume that we need to sort an array of integers by natural order (primitive int). For this, we can rely on Arrays.sort(int[] a), as in the following example:

int[] integers = new int[]{...};
Arrays.sort(integers);

Sometimes, we need to sort an array of an object. Let's assume that we have a class as Melon:

public class Melon {

private final String type;
private final int weight;

public Melon(String type, int weight) {
this.type = type;
this.weight = weight;
}

// getters omitted for brevity
}

An array of Melon can be sorted by ascending weight via the proper Comparator:

Melon[] melons = new Melon[] { ... };

Arrays.sort(melons, new Comparator<Melon>() {
@Override
public int compare(Melon melon1, Melon melon2) {
return Integer.compare(melon1.getWeight(), melon2.getWeight());
}
});

The same result can be obtained by rewriting the preceding code via a lambda expression:

Arrays.sort(melons, (Melon melon1, Melon melon2) 
-> Integer.compare(melon1.getWeight(), melon2.getWeight()));

Moreover, arrays provide a method for sorting elements in parallel, parallelSort(). The sorting algorithm used behind the scenes is a parallel sort-merge based on ForkJoinPool that breaks up the array into sub-arrays that are themselves sorted and then merged. Here is an example:

Arrays.parallelSort(melons, new Comparator<Melon>() {
@Override
public int compare(Melon melon1, Melon melon2) {
return Integer.compare(melon1.getWeight(), melon2.getWeight());
}
});

Or, via a lambda expression, we have the following example:

Arrays.parallelSort(melons, (Melon melon1, Melon melon2) 
-> Integer.compare(melon1.getWeight(), melon2.getWeight()));

The preceding examples sort an array in ascending order, but sometimes, we need to sort it by descending order. When we sort an array of Object and rely on a Comparator, we can simply multiply the result returned by Integer.compare() by -1:

Arrays.sort(melons, new Comparator<Melon>() {
@Override
public int compare(Melon melon1, Melon melon2) {
return (-1) * Integer.compare(melon1.getWeight(),
melon2.getWeight());
}
});

Or, we can simply switch the arguments in the compare() method.

In the case of an array of boxed primitive types, the solution can rely on the Collections.reverse() method, as in the following example:

Integer[] integers = new Integer[] {3, 1, 5};

// 1, 3, 5
Arrays.sort(integers);

// 5, 3, 1
Arrays.sort(integers, Collections.reverseOrder());

Unfortunately, there is no built-in solution for sorting an array of primitives in descending order. Most commonly, if we still want to rely on Arrays.sort(), the solution to this problem consists of reversing the array (O(n)) after it is sorted in ascending order:

// sort ascending
Arrays.sort(integers);

// reverse array to obtain it in descending order
for (int leftHead = 0, rightHead = integers.length - 1;
leftHead < rightHead; leftHead++, rightHead--) {

int elem = integers[leftHead];
integers[leftHead] = integers[rightHead];
integers[rightHead] = elem;
}

Another solution can rely on Java 8 functional style and boxing (be aware that boxing is a pretty time-consuming operation):

int[] descIntegers = Arrays.stream(integers)
.boxed() //or .mapToObj(i -> i)
.sorted((i1, i2) -> Integer.compare(i2, i1))
.mapToInt(Integer::intValue)
.toArray();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.19.174