Skip to main content

Sorting and Searching Arrays in Java

Introduction​

Sorting and searching are common operations performed on arrays in Java. Sorting arranges the elements of an array in a specific order, while searching finds the position of a particular element in the array.

Sorting Arrays​

Arrays.sort() Method​

The Arrays.sort() method is used to sort arrays in ascending order. For arrays of primitive types, it uses a modified quicksort algorithm. For arrays of objects, it uses the natural ordering of the elements or a specified comparator.

Example​

import java.util.Arrays;

public class Main {
public static void main(String[] args) {
int[] numbers = {5, 2, 7, 1, 9};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers)); // Outputs: [1, 2, 5, 7, 9]
}
}

Sorting Arrays in Descending Order​

To sort arrays in descending order, you can use the Comparator.reverseOrder() method along with Arrays.sort() for arrays of objects, or you can reverse the array after sorting for arrays of primitive types.

Example​

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

public class Main {
public static void main(String[] args) {
Integer[] numbers = {5, 2, 7, 1, 9};
Arrays.sort(numbers, Comparator.reverseOrder());
System.out.println(Arrays.toString(numbers)); // Outputs: [9, 7, 5, 2, 1]
}
}

Sorting Arrays of Objects​

When sorting arrays of objects, the objects must implement the Comparable interface or you must provide a custom Comparator.

Example​

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

class Person implements Comparable<Person> {
private String name;
private int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

@Override
public int compareTo(Person other) {
return Integer.compare(this.age, other.age);
}

@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

public class Main {
public static void main(String[] args) {
Person[] people = {
new Person("Alice", 25),
new Person("Bob", 30),
new Person("Charlie", 20)
};

Arrays.sort(people);
System.out.println(Arrays.toString(people)); // Sorted by age
}
}

Searching Arrays​

Arrays.binarySearch() Method​

The Arrays.binarySearch() method is used to search for a specified element in a sorted array. It returns the index of the element if found, otherwise it returns a negative value.

Example​

import java.util.Arrays;

public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int index = Arrays.binarySearch(numbers, 5);
System.out.println("Index of 5: " + index); // Outputs: 4
}
}

Note​

  • The array must be sorted before using Arrays.binarySearch().
  • If the element is not found, the method returns the insertion point (negative value).

Conclusion​

Sorting and searching are essential operations performed on arrays in Java. The Arrays.sort() method is used to sort arrays, and Arrays.binarySearch() is used to search for elements in sorted arrays. Understanding these operations allows you to efficiently manage and manipulate array data in Java.