sort() – sort array

The Arrays.sort() method in Java is a static method in the java.util.Arrays class that sorts the elements of an array. It provides a convenient way to sort arrays of both primitive types and objects. The method is overloaded to handle different types of arrays, including arrays of primitive types (e.g., int, char, double) and arrays of objects.

Key Points of Arrays.sort()

  1. In-Place Sorting:
    • The method sorts the array in place, meaning it modifies the original array and does not create a new array.
  2. Default Sorting Order:
    • For primitive types, the elements are sorted in ascending order.
    • For objects, the elements are sorted according to their natural ordering, which is defined by the compareTo() method of the Comparable interface.
    • You can also provide a custom comparator to define a different order for objects.
  3. Time Complexity:
    • The sorting algorithm used is a Dual-Pivot Quicksort for primitive types and TimSort for objects, both of which have an average time complexity of O(n log n).

Overloaded Methods

Here are the main overloaded variants of the Arrays.sort() method:

  • public static void sort(byte[] a)
  • public static void sort(char[] a)
  • public static void sort(double[] a)
  • public static void sort(float[] a)
  • public static void sort(int[] a)
  • public static void sort(long[] a)
  • public static void sort(short[] a)
  • public static <T> void sort(T[] a)
  • public static <T> void sort(T[] a, Comparator<? super T> c)

Examples

Sorting Primitive Type Arrays

import java.util.Arrays;

public class ArraySortExample {
    public static void main(String[] args) {
        int[] intArray = {5, 3, 8, 1, 2};
        double[] doubleArray = {3.3, 1.1, 2.2, 5.5, 4.4};
        char[] charArray = {'d', 'a', 'c', 'b', 'e'};

        // Sorting primitive type arrays
        Arrays.sort(intArray);
        Arrays.sort(doubleArray);
        Arrays.sort(charArray);

        // Printing sorted arrays
        System.out.println("Sorted intArray: " + Arrays.toString(intArray)); // Output: [1, 2, 3, 5, 8]
        System.out.println("Sorted doubleArray: " + Arrays.toString(doubleArray)); // Output: [1.1, 2.2, 3.3, 4.4, 5.5]
        System.out.println("Sorted charArray: " + Arrays.toString(charArray)); // Output: [a, b, c, d, e]
    }
}

Sorting Object Type Arrays

import java.util.Arrays;

public class ArraySortExample {
    public static void main(String[] args) {
        String[] stringArray = {"banana", "apple", "cherry", "date"};

        // Sorting object type array
        Arrays.sort(stringArray);

        // Printing sorted array
        System.out.println("Sorted stringArray: " + Arrays.toString(stringArray)); // Output: [apple, banana, cherry, date]
    }
}

Sorting with Custom Comparator

For object arrays, you can define a custom order by using a Comparator.

import java.util.Arrays;
import java.util.Comparator;

public class ArraySortExample {
    public static void main(String[] args) {
        String[] stringArray = {"banana", "apple", "cherry", "date"};

        // Sorting with a custom comparator (reverse order)
        Arrays.sort(stringArray, Comparator.reverseOrder());

        // Printing sorted array
        System.out.println("Sorted stringArray (reverse order): " + Arrays.toString(stringArray)); // Output: [date, cherry, banana, apple]
    }
}

Sorting a Subarray

You can also sort a subarray using the overloaded Arrays.sort() method that accepts fromIndex and toIndex parameters.

import java.util.Arrays;

public class ArraySortExample {
    public static void main(String[] args) {
        int[] intArray = {5, 3, 8, 1, 2, 9, 4, 7, 6};

        // Sorting a subarray from index 2 (inclusive) to index 7 (exclusive)
        Arrays.sort(intArray, 2, 7);

        // Printing sorted array
        System.out.println("Partially sorted intArray: " + Arrays.toString(intArray)); // Output: [5, 3, 1, 2, 8, 9, 4, 7, 6]
    }
}

Important Considerations

  • Stability: The sort is stable for object arrays, meaning that equal elements maintain their relative order.
  • Performance: While the average time complexity is O(n log n), the performance can degrade to O(n²) in the worst case for certain inputs with the Dual-Pivot Quicksort used for primitive types.
  • Large Arrays: For large arrays, ensure that you have sufficient memory and consider the performance implications of sorting operations.

In summary, Arrays.sort() is a powerful and flexible method for sorting arrays in Java, offering various overloaded versions to handle different types of arrays and providing options for custom sorting orders through comparators.