{"record":{"id":"77d5f39021f552b6","repo":"TheAlgorithms/Java","slug":"beadsort-cannot-sort-negative-numbers","errorCode":null,"errorMessage":"BeadSort cannot sort negative numbers.","messagePattern":"BeadSort cannot sort negative numbers\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/sorts/BeadSort.java","lineNumber":22,"sourceCode":"\npublic class BeadSort {\n    private enum BeadState { BEAD, EMPTY }\n\n    /**\n     * Sorts the given array using the BeadSort algorithm.\n     *\n     * @param array The array of non-negative integers to be sorted.\n     * @return The sorted array.\n     * @throws IllegalArgumentException If the array contains negative numbers.\n     */\n    public int[] sort(int[] array) {\n        allInputsMustBeNonNegative(array);\n        return extractSortedFromGrid(fillGrid(array));\n    }\n\n    private void allInputsMustBeNonNegative(final int[] array) {\n        if (Arrays.stream(array).anyMatch(s -> s < 0)) {\n            throw new IllegalArgumentException(\"BeadSort cannot sort negative numbers.\");\n        }\n    }\n\n    private BeadState[][] fillGrid(final int[] array) {\n        final var maxValue = Arrays.stream(array).max().orElse(0);\n        var grid = getEmptyGrid(array.length, maxValue);\n\n        int[] count = new int[maxValue];\n        for (int i = 0, arrayLength = array.length; i < arrayLength; i++) {\n            int k = 0;\n            for (int j = 0; j < array[i]; j++) {\n                grid[count[maxValue - k - 1]++][k] = BeadState.BEAD;\n                k++;\n            }\n        }\n        return grid;\n    }\n","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/sorts/BeadSort.java#L4-L40","documentation":"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.","triggerScenarios":"Calling sort(new int[]{3, -1, 2}); passing data containing negative deltas or signed measurements; merging arrays where one had negatives.","commonSituations":"Financial data with negative balances; temperature/sensor readings below zero; using BeadSort generically without checking the data domain.","solutions":["Filter out or reject negative values before calling sort().","If negatives must be sorted, choose a comparison-based sort (e.g. Arrays.sort).","Offset all values by a constant to make them non-negative if the range is known and bounded."],"exampleFix":"// before\nint[] sorted = new BeadSort().sort(data);\n\n// after\nif (Arrays.stream(data).anyMatch(v -> v < 0)) throw new IllegalArgumentException(\"negatives not supported\");\nint[] sorted = new BeadSort().sort(data);","handlingStrategy":"validation","validationCode":"if (Arrays.stream(array).anyMatch(v -> v < 0)) throw new IllegalArgumentException(\"negatives not supported by BeadSort\");","typeGuard":"public static boolean isAllNonNegative(int[] a) { return IntStream.of(a).allMatch(v -> v >= 0); }","tryCatchPattern":null,"preventionTips":["Restrict BeadSort to non-negative domains or choose a comparison sort.","Offset signed data by a known constant before sorting when the range is bounded.","Add a domain precondition check at the data-loading boundary."],"tags":["sorting","non-negative-domain","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}