{"record":{"id":"90c4c31f23b7f79f","repo":"TheAlgorithms/Java","slug":"minimum-number-of-buckets-must-be-between-1-and-10","errorCode":null,"errorMessage":"Minimum number of buckets must be between 1 and 100","messagePattern":"Minimum number of buckets must be between 1 and 100","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/sorts/SpreadSort.java","lineNumber":34,"sourceCode":"    private final int initialBucketCapacity;\n    private final int minBuckets;\n\n    /**\n     * Constructor to initialize the SpreadSort algorithm with custom parameters.\n     *\n     * @param insertionSortThreshold the threshold for using insertion sort for small segments (1-1000)\n     * @param initialBucketCapacity  the initial capacity for each bucket (1-1000)\n     * @param minBuckets             the minimum number of buckets to use (1-100)\n     */\n    public SpreadSort(int insertionSortThreshold, int initialBucketCapacity, int minBuckets) {\n        if (insertionSortThreshold < 1 || insertionSortThreshold > MAX_INSERTION_SORT_THRESHOLD) {\n            throw new IllegalArgumentException(\"Insertion sort threshold must be between 1 and \" + MAX_INSERTION_SORT_THRESHOLD);\n        }\n        if (initialBucketCapacity < 1 || initialBucketCapacity > MAX_INITIAL_BUCKET_CAPACITY) {\n            throw new IllegalArgumentException(\"Initial bucket capacity must be between 1 and \" + MAX_INITIAL_BUCKET_CAPACITY);\n        }\n        if (minBuckets < 1 || minBuckets > MAX_MIN_BUCKETS) {\n            throw new IllegalArgumentException(\"Minimum number of buckets must be between 1 and \" + MAX_MIN_BUCKETS);\n        }\n\n        this.insertionSortThreshold = insertionSortThreshold;\n        this.initialBucketCapacity = initialBucketCapacity;\n        this.minBuckets = minBuckets;\n    }\n\n    /**\n     * Default constructor with predefined values.\n     */\n    public SpreadSort() {\n        this(16, 16, 2);\n    }\n\n    /**\n     * Sorts an array using the SpreadSort algorithm.\n     *\n     * @param array the array to be sorted","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/sorts/SpreadSort.java#L16-L52","documentation":"SpreadSort is a bucket/distribution sort whose constructor validates tuning parameters. The third parameter, minBuckets, sets the minimum number of buckets the algorithm will use; it must fall in [1, MAX_MIN_BUCKETS] where MAX_MIN_BUCKETS = 100. Passing a value outside that range throws IllegalArgumentException because the algorithm cannot partition work with zero or an unbounded number of buckets.","triggerScenarios":"Calling `new SpreadSort(insertionSortThreshold, initialBucketCapacity, minBuckets)` with minBuckets <= 0 (e.g. 0 or negative) or minBuckets > 100 (e.g. 101, 500). The no-arg `new SpreadSort()` defaults to minBuckets=2 and is always safe.","commonSituations":"Deriving minBuckets dynamically from input size without clamping (e.g. `new SpreadSort(16, 16, n/10)` for very small n yielding 0); copying an example that used a different MAX_MIN_BUCKETS constant; off-by-one when auto-tuning bucket counts from array length.","solutions":["Pass a minBuckets value within 1..100 (the no-arg `new SpreadSort()` uses 2).","If computing minBuckets from input size, clamp it: `Math.min(100, Math.max(1, computed))`.","Verify the three constants MAX_INSERTION_SORT_THRESHOLD, MAX_INITIAL_BUCKET_CAPACITY, MAX_MIN_BUCKETS match the ranges you assume before constructing."],"exampleFix":"// before\nnew SpreadSort(16, 16, 0);   // throws: minBuckets out of range\n\n// after\nnew SpreadSort(16, 16, Math.min(100, Math.max(1, n / 10)));\n// or simply\nnew SpreadSort();","handlingStrategy":"validation","validationCode":"private static final int MAX_MIN_BUCKETS = 100;\nstatic SpreadSort safeSort(int threshold, int capacity, int minBuckets) {\n    if (minBuckets < 1 || minBuckets > MAX_MIN_BUCKETS) {\n        throw new IllegalArgumentException(\n            \"minBuckets \" + minBuckets + \" out of range [1, \" + MAX_MIN_BUCKETS + \"]\");\n    }\n    return new SpreadSort(threshold, capacity, minBuckets);\n}","typeGuard":"static boolean isValidMinBuckets(int minBuckets) {\n    return minBuckets >= 1 && minBuckets <= 100;\n}","tryCatchPattern":null,"preventionTips":["Always clamp dynamically-computed bucket counts: Math.min(100, Math.max(1, value)).","Prefer the no-arg constructor unless you have measured reason to tune.","Keep a single named constant for the upper bound shared with your callers."],"tags":["sorting","bucket-sort","constructor","validation","illegalargument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}