Sub topic 10
Sorting and Searching Arrays
Sorting puts values in order. Searching finds a value you need. Java provides both actions through Arrays, a class with methods for working with arrays. A method is a named action that a program can run.
An array is a fixed-size sequence of values. Each value is called an element. An index is an element’s position, starting at 0.
Sort a copy, then search it
Arrays.sort puts numbers in ascending order, from smallest to largest. It changes the array you give it.
Arrays.binarySearch looks for a value in an array that is already sorted. It repeatedly narrows the part of the array it needs to check.
The following program copies an array before sorting it, so the original stays in its first order. Save it as SortAndSearch.java, then compile and run it.
import java.util.Arrays;
public class SortAndSearch {
public static void main(String[] args) {
int[] original = {8, 1, 5, 3};
int[] sorted = Arrays.copyOf(original, original.length);
Arrays.sort(sorted);
int found = Arrays.binarySearch(sorted, 5);
int missing = Arrays.binarySearch(sorted, 4);
System.out.println("Original: " + Arrays.toString(original));
System.out.println("Sorted: " + Arrays.toString(sorted));
System.out.println("Index of 5: " + found);
System.out.println("Search result for 4: " + missing);
System.out.println("Insertion point for 4: " + (-missing - 1));
}
}The output is:
- Original:
[8, 1, 5, 3] - Sorted:
[1, 3, 5, 8] - Index of 5:
2 - Search result for 4:
-3 - Insertion point for 4:
2
Arrays.copyOf creates the separate array named sorted. The call to Arrays.sort changes that copy, not original. Arrays.toString makes the array contents readable when printed.
Understand the search result
When binarySearch finds the value, it returns an index. In the example, 5 is at index 2 in sorted. That index refers to the sorted copy, not to the original array.
When the value is missing, the result is negative. It tells you where the value could be placed while keeping the array sorted. That position is called the insertion point.
For [1, 3, 5, 8], the insertion point for 4 is index 2, between 3 and 5. The search returns the negative insertion point minus one: -2 - 1, or -3.
To get the insertion point from a negative result, use -result - 1. Only do this after checking that the result is negative.
If the same value occurs more than once, binarySearch may return the index of any matching element. It does not promise the first or last match.
Choose an approach
Sorting is useful when you need the values in order or plan to search the same array many times. Remember that sorting takes work and changes the array you sort.
For a single search in an unsorted array, it may be simpler to check each element in order. This is called a linear search. It does not require sorting, so it also preserves the array’s current order.
Practice
Exercise
After sorting [7, -2, 4], what does binarySearch return for 4? What does it return for missing value 3?
Show answer
The sorted array is [-2, 4, 7]. The search for 4 returns 1. The insertion point for 3 is 1, so the search returns -2.