{"record":{"id":"044b6a2e03188bf1","repo":"TheAlgorithms/Java","slug":"input-array-must-be-sorted","errorCode":null,"errorMessage":"Input array must be sorted.","messagePattern":"Input array must be sorted\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/searches/FibonacciSearch.java","lineNumber":36,"sourceCode":"@SuppressWarnings({\"rawtypes\", \"unchecked\"})\npublic class FibonacciSearch implements SearchAlgorithm {\n\n    /**\n     * Finds the index of the specified key in a sorted array using Fibonacci search.\n     *\n     * @param array The sorted array to search.\n     * @param key The element to search for.\n     * @param <T> The type of the elements in the array, which must be comparable.\n     * @throws IllegalArgumentException if the input array is not sorted or empty, or if the key is null.\n     * @return The index of the key if found, otherwise -1.\n     */\n    @Override\n    public <T extends Comparable<T>> int find(T[] array, T key) {\n        if (array.length == 0) {\n            throw new IllegalArgumentException(\"Input array must not be empty.\");\n        }\n        if (!isSorted(array)) {\n            throw new IllegalArgumentException(\"Input array must be sorted.\");\n        }\n        if (key == null) {\n            throw new IllegalArgumentException(\"Key must not be null.\");\n        }\n\n        int fibMinus1 = 1;\n        int fibMinus2 = 0;\n        int fibNumber = fibMinus1 + fibMinus2;\n        int n = array.length;\n\n        while (fibNumber < n) {\n            fibMinus2 = fibMinus1;\n            fibMinus1 = fibNumber;\n            fibNumber = fibMinus2 + fibMinus1;\n        }\n\n        int offset = -1;\n","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/searches/FibonacciSearch.java#L18-L54","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Sort the array with Arrays.sort(array) (matching the Comparable contract) before calling find().","If the array may be partially ordered, copy and sort the copy before searching.","Verify ordering upstream and document the sorted precondition at your API boundary."],"exampleFix":"// before\nint idx = new FibonacciSearch().find(arr, key);\n\n// after\nArrays.sort(arr);\nint idx = new FibonacciSearch().find(arr, key);","handlingStrategy":"validation","validationCode":"if (!isSorted(array)) Arrays.sort(array);","typeGuard":"public static <T extends Comparable<T>> boolean isSortedAscending(T[] a) {\n    for (int i = 1; i < a.length; i++) if (a[i - 1].compareTo(a[i]) > 0) return false;\n    return true;\n}","tryCatchPattern":null,"preventionTips":["Sort with the same ordering (natural Comparable) you will search with.","Treat the array as effectively immutable between sort and search.","Add a sortedness assertion in tests covering the search pipeline."],"tags":["search","precondition","sorted-input","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}