Arrays.sort() / Arrays.binarySearch()
Methods for sorting arrays and performing fast binary searches on sorted arrays. These are provided as static methods of the java.util.Arrays class.
Syntax
import java.util.Arrays; // Sorts an array in ascending order (destructive operation). Arrays.sort(array); Arrays.sort(array, int fromIndex, int toIndex); // Sorts an object array using a custom comparator. Arrays.sort(T[] array, Comparator<? super T> c); // Performs a binary search on a sorted array. Arrays.binarySearch(array, key); Arrays.binarySearch(array, int fromIndex, int toIndex, key);
Method List
| Method | Description |
|---|---|
| Arrays.sort(array) | Sorts the entire array in ascending order. Uses Dual-Pivot Quicksort for primitive types and TimSort for object types. |
| Arrays.sort(array, fromIndex, toIndex) | Sorts only the range from fromIndex up to, but not including, toIndex. |
| Arrays.sort(T[] array, Comparator c) | Sorts an object array in a custom order using the specified Comparator. |
| Arrays.binarySearch(array, key) | Searches a sorted array for key using binary search. Returns the index if found, or a negative value if not found. |
Sample Code
import java.util.Arrays;
// Sort an int array in ascending order.
int[] nums = {5, 2, 8, 1, 9, 3};
Arrays.sort(nums);
System.out.println(Arrays.toString(nums)); // Prints "[1, 2, 3, 5, 8, 9]".
// Sort only a portion of the array.
int[] partial = {5, 2, 8, 1, 9, 3};
Arrays.sort(partial, 1, 4); // Sorts only indexes 1–3 (2, 8, 1).
System.out.println(Arrays.toString(partial)); // Prints "[5, 1, 2, 8, 9, 3]".
// Sort a String array.
String[] fruits = {"banana", "apple", "cherry"};
Arrays.sort(fruits);
System.out.println(Arrays.toString(fruits)); // Prints "[apple, banana, cherry]".
// Sort in descending order using a Comparator.
String[] langs = {"Java", "Python", "C", "Swift"};
Arrays.sort(langs, (a, b) -> b.compareTo(a));
System.out.println(Arrays.toString(langs)); // Prints the array in descending order.
// Search for a value with binarySearch() — always sort the array first.
int[] sorted = {1, 3, 5, 7, 9};
int idx = Arrays.binarySearch(sorted, 7);
System.out.println(idx); // Prints "3".
// Returns a negative value when the element is not found.
System.out.println(Arrays.binarySearch(sorted, 4)); // Prints a negative value.
Notes
Arrays.sort() is a static method that sorts an array in ascending order. Note that it modifies the original array directly (destructive operation). If you need to preserve the original order, create a copy of the array beforehand.
Arrays.binarySearch() searches for a value in O(log n) time using binary search. Binary search requires the array to be sorted in ascending order. Using it on an unsorted array may return incorrect results. When the element is not found, the return value is a negative integer in the form -(insertion point) - 1.
For copying and filling arrays, see Arrays.copyOf() / Arrays.fill(). For comparing arrays and converting them to strings, see Arrays.equals() / Arrays.toString().
If you find any errors or copyright issues, please contact us.