TheAlgorithms/Java · error · IllegalArgumentException

Input arrays are not sorted

Error message

Input arrays are not sorted

What it means

findMedianSortedArrays uses a binary-search-over-partition algorithm that is guaranteed to find a valid partition if and only if both input arrays are individually sorted in non-decreasing order. If the while loop exits without returning, the precondition was violated, so the method throws IllegalArgumentException indicating the arrays are not sorted.

Solutions

  1. Sort both arrays with Arrays.sort() before calling findMedianSortedArrays.
  2. Validate sortedness with a pre-condition check before the call.
  3. Review upstream data pipeline to ensure sorting is preserved end-to-end.

Example fix

// before
double m = MedianOfTwoSortedArrays.findMedianSortedArrays(
    new int[]{3,1,2}, new int[]{6,5,4}); // throws

// after
int[] a = {3,1,2};
int[] b = {6,5,4};
Arrays.sort(a);
Arrays.sort(b);
double m = MedianOfTwoSortedArrays.findMedianSortedArrays(a, b);
Defensive patterns

Strategy: validation

Validate before calling

static boolean isSorted(int[] arr) {
    for (int i = 1; i < arr.length; i++) {
        if (arr[i - 1] > arr[i]) return false;
    }
    return true;
}
// usage:
if (!isSorted(nums1) || !isSorted(nums2)) {
    Arrays.sort(nums1);
    Arrays.sort(nums2);
}
double median = MedianOfTwoSortedArrays.findMedianSortedArrays(nums1, nums2);

Type guard

static boolean areBothSorted(int[] a, int[] b) {
    return isSorted(a) && isSorted(b);
}

Try / catch

try {
    median = MedianOfTwoSortedArrays.findMedianSortedArrays(nums1, nums2);
} catch (IllegalArgumentException e) {
    Arrays.sort(nums1);
    Arrays.sort(nums2);
    median = MedianOfTwoSortedArrays.findMedianSortedArrays(nums1, nums2);
}

Prevention

When it happens

Trigger: Passing arrays where at least one is not sorted in non-decreasing order, or arrays sorted in descending order. The binary search never converges on a valid partition and the loop terminates normally.

Common situations: Feeding raw unsorted data from a database or sensor stream; accidentally reversing an array; using descending-sorted data when ascending is required.

Related errors


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

Appendix: source

Thrown at src/main/java/com/thealgorithms/divideandconquer/MedianOfTwoSortedArrays.java:51

            // Check if partition is valid
            if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) {
                // If combined array length is odd
                if (((m + n) & 1) == 1) {
                    return Math.max(maxLeft1, maxLeft2);
                }
                // If combined array length is even
                else {
                    return (Math.max(maxLeft1, maxLeft2) + Math.min(minRight1, minRight2)) / 2.0;
                }
            } else if (maxLeft1 > minRight2) {
                high = partition1 - 1;
            } else {
                low = partition1 + 1;
            }
        }

        throw new IllegalArgumentException("Input arrays are not sorted");
    }
}

View on GitHub (pinned to fdfb9a395b)