TheAlgorithms/Java · error · IllegalArgumentException

Array contains non-positive integers.

Error message

Array contains non-positive integers.

What it means

Thrown by RadixSort.checkForNegativeInput(int[]) when any element is negative. This implementation uses counting sort per digit starting from the least significant digit and assumes non-negative values; negative numbers would be misclassified because their digit extraction differs. Note: the message says 'non-positive' but the code only checks number < 0, so zero is accepted.

Source

Thrown at src/main/java/com/thealgorithms/sorts/RadixSort.java:42

        if (array.length == 0) {
            return array;
        }

        checkForNegativeInput(array);
        radixSort(array);
        return array;
    }

    /**
     * Checks if the array contains any negative integers.
     *
     * @param array the array to be checked
     * @throws IllegalArgumentException if any negative integers are found
     */
    private static void checkForNegativeInput(int[] array) {
        for (int number : array) {
            if (number < 0) {
                throw new IllegalArgumentException("Array contains non-positive integers.");
            }
        }
    }

    private static void radixSort(int[] array) {
        final int max = Arrays.stream(array).max().getAsInt();
        for (int i = 0, exp = 1; i < NumberOfDigits.numberOfDigits(max); i++, exp *= BASE) {
            countingSortByDigit(array, exp);
        }
    }

    /**
     * A utility method to perform counting sort of array[] according to the digit represented by exp.
     *
     * @param array the array to be sorted
     * @param exp   the exponent representing the current digit position
     */
    private static void countingSortByDigit(int[] array, int exp) {

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate all elements >= 0 before calling sort.
  2. For signed data, partition negatives and positives, sort each, then combine; or use Arrays.sort.
  3. If the range is bounded, offset values to make them non-negative before sorting.

Example fix

// before
RadixSort.sort(data);

// after
if (Arrays.stream(data).anyMatch(v -> v < 0)) throw new IllegalArgumentException("negatives not supported");
RadixSort.sort(data);
Defensive patterns

Strategy: validation

Validate before calling

if (Arrays.stream(array).anyMatch(v -> v < 0)) throw new IllegalArgumentException("negatives not supported by this RadixSort");

Type guard

public static boolean isAllNonNegative(int[] a) { return Arrays.stream(a).allMatch(v -> v >= 0); }

Prevention

When it happens

Trigger: Calling sort with an array containing a negative value such as {4, -2, 7}; sorting signed data from sensors or deltas.

Common situations: Signed measurement data; using this RadixSort on general integers; assuming radix sort handles negatives like comparison sorts.

Related errors


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