TheAlgorithms/Java · error · IllegalArgumentException

BeadSort cannot sort negative numbers.

Error message

BeadSort cannot sort negative numbers.

What it means

Thrown by BeadSort.sort(int[]) when any element is negative. BeadSort models each number as a count of beads on an abacus, which only works for non-negative integers; negative counts have no physical analog in the algorithm. The allInputsMustBeNonNegative helper scans the array before building the grid.

Source

Thrown at src/main/java/com/thealgorithms/sorts/BeadSort.java:22

public class BeadSort {
    private enum BeadState { BEAD, EMPTY }

    /**
     * Sorts the given array using the BeadSort algorithm.
     *
     * @param array The array of non-negative integers to be sorted.
     * @return The sorted array.
     * @throws IllegalArgumentException If the array contains negative numbers.
     */
    public int[] sort(int[] array) {
        allInputsMustBeNonNegative(array);
        return extractSortedFromGrid(fillGrid(array));
    }

    private void allInputsMustBeNonNegative(final int[] array) {
        if (Arrays.stream(array).anyMatch(s -> s < 0)) {
            throw new IllegalArgumentException("BeadSort cannot sort negative numbers.");
        }
    }

    private BeadState[][] fillGrid(final int[] array) {
        final var maxValue = Arrays.stream(array).max().orElse(0);
        var grid = getEmptyGrid(array.length, maxValue);

        int[] count = new int[maxValue];
        for (int i = 0, arrayLength = array.length; i < arrayLength; i++) {
            int k = 0;
            for (int j = 0; j < array[i]; j++) {
                grid[count[maxValue - k - 1]++][k] = BeadState.BEAD;
                k++;
            }
        }
        return grid;
    }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Filter out or reject negative values before calling sort().
  2. If negatives must be sorted, choose a comparison-based sort (e.g. Arrays.sort).
  3. Offset all values by a constant to make them non-negative if the range is known and bounded.

Example fix

// before
int[] sorted = new BeadSort().sort(data);

// after
if (Arrays.stream(data).anyMatch(v -> v < 0)) throw new IllegalArgumentException("negatives not supported");
int[] sorted = new BeadSort().sort(data);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: Calling sort(new int[]{3, -1, 2}); passing data containing negative deltas or signed measurements; merging arrays where one had negatives.

Common situations: Financial data with negative balances; temperature/sensor readings below zero; using BeadSort generically without checking the data domain.

Related errors


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