TheAlgorithms/Java · error · IllegalArgumentException

Input array must be sorted.

Error message

Input array must be sorted.

What it means

Thrown by FibonacciSearch.find(T[], T) when isSorted(array) returns false. Fibonacci search assumes a sorted array to correctly narrow the search range using Fibonacci numbers; an unsorted array yields garbage results, so the method rejects it early.

Source

Thrown at src/main/java/com/thealgorithms/searches/FibonacciSearch.java:36

@SuppressWarnings({"rawtypes", "unchecked"})
public class FibonacciSearch implements SearchAlgorithm {

    /**
     * Finds the index of the specified key in a sorted array using Fibonacci search.
     *
     * @param array The sorted array to search.
     * @param key The element to search for.
     * @param <T> The type of the elements in the array, which must be comparable.
     * @throws IllegalArgumentException if the input array is not sorted or empty, or if the key is null.
     * @return The index of the key if found, otherwise -1.
     */
    @Override
    public <T extends Comparable<T>> int find(T[] array, T key) {
        if (array.length == 0) {
            throw new IllegalArgumentException("Input array must not be empty.");
        }
        if (!isSorted(array)) {
            throw new IllegalArgumentException("Input array must be sorted.");
        }
        if (key == null) {
            throw new IllegalArgumentException("Key must not be null.");
        }

        int fibMinus1 = 1;
        int fibMinus2 = 0;
        int fibNumber = fibMinus1 + fibMinus2;
        int n = array.length;

        while (fibNumber < n) {
            fibMinus2 = fibMinus1;
            fibMinus1 = fibNumber;
            fibNumber = fibMinus2 + fibMinus1;
        }

        int offset = -1;

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Sort the array with Arrays.sort(array) (matching the Comparable contract) before calling find().
  2. If the array may be partially ordered, copy and sort the copy before searching.
  3. Verify ordering upstream and document the sorted precondition at your API boundary.

Example fix

// before
int idx = new FibonacciSearch().find(arr, key);

// after
Arrays.sort(arr);
int idx = new FibonacciSearch().find(arr, key);
Defensive patterns

Strategy: validation

Validate before calling

if (!isSorted(array)) Arrays.sort(array);

Type guard

public static <T extends Comparable<T>> boolean isSortedAscending(T[] a) {
    for (int i = 1; i < a.length; i++) if (a[i - 1].compareTo(a[i]) > 0) return false;
    return true;
}

Prevention

When it happens

Trigger: Passing an array that is not in ascending order per Comparable natural ordering; passing a descending-sorted array; mutating the array between sort and search; using a Comparator that does not match the sort order.

Common situations: Sorting with one Comparator and searching with natural ordering; receiving data from a source that does not guarantee order; race conditions where another thread reorders the array.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/044b6a2e03188bf1. Report an issue: GitHub.