Sorting and Selection of Arrays

There is so much logics regarding sorting an array but java provide a variety methods to sort array in util package.

Sorting Arrays with Arrays.sort(...)

The “java.util.Arrays” class has many static functions for sorting arrays. It can sort both type of array primitive types as well as object types. Function can apply both way, on entire array as well as partial array components. For object types you have comparator to define how the sort should be performed.

Method

Description

Arrays.sort(pa);

Sorts the primitive type array into ascending order using their natural ordering.

Arrays.sort(pa, from, to);

Sorts the array elements pa[from]...pa[to-1] of a primitive type into ascending order.

Arrays.sort(oa);

Sorts the object type array into ascending order, using the Comparable interface, which defines the compareTo() method.

Arrays.sort(oa, from, to);

Sorts array, in the range from...to of an object type into ascending order.

Arrays.sort(oa, comp);

Sorts the object type array into ascending order, using the Comparator comp.

Arrays.sort(oa, from, to, comp);

Sorts the elements of the array, in the range from...to of an object type into ascending order using the Comparator comp.

Example - Sorting arrays using Arrays.sort()

This is the example which sorts an array of String type and an array of double type. All object types that implement Comparable (ie, defines compareTo() method), can be sorted with using a comparator. The Arrays.sort() method is also introduced for primitive arrays. Get all the help you need regarding theassignmentandhomeworkatTranstutors.com.

import java.util.Arrays;

public class DoubleSort {

//========================================================= main

public static void main(String[] args) {

// Sort strings - or any other Comparable objects.

String[] names = {"Ram", "Sham", "Ajay"};

Arrays.sort(names);

System.out.println(Arrays.toString(names));

//Sort doubles or other primitives.

double[] length = {120.0, 0.5, 0.0, 999.0, 77.3};

Arrays.sort(length);

System.out.println(Arrays.toString(length));

}

}

Output:

[Steve, Mark, John]

[0.0, 0.5, 77.3, 120.0, 999.0]

Comparators can define sort order

Java uses comparison sorts, that mean they can make all ordering decisions by comparing two values. If there is no "natural" ordering. You can specify a Comparator to use in comparing two values.

The above program produce randomly element from array type and select and sort it with the selection methods. Get all the help you need regarding theassignmentandhomeworkatTranstutors.com

Get all the help you need regarding theassignmentandhomeworkatTranstutors.com. We are considered pioneer in providing all types ofassignment helpandhomework helpof any school grade level, to undergraduate and graduate level. You can be sure that all our assignments would be original and plagiarism free.

Our experienced team will provide all thehomework helpandassignment helpas per the guidelines provided by the student within the given deadline. All theassignment helpandhomework helpare provided with good number of references by our team.