{"record":{"id":"3c308992ac70541d","repo":"TheAlgorithms/Java","slug":"insertion-sort-threshold-must-be-between-1-and-100","errorCode":null,"errorMessage":"Insertion sort threshold must be between 1 and 1000","messagePattern":"Insertion sort threshold must be between 1 and 1000","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/sorts/SpreadSort.java","lineNumber":28,"sourceCode":"public class SpreadSort implements SortAlgorithm {\n    private static final int MAX_INSERTION_SORT_THRESHOLD = 1000;\n    private static final int MAX_INITIAL_BUCKET_CAPACITY = 1000;\n    private static final int MAX_MIN_BUCKETS = 100;\n\n    private final int insertionSortThreshold;\n    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);","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/sorts/SpreadSort.java#L10-L46","documentation":"Thrown by the SpreadSort(int, int, int) constructor when insertionSortThreshold < 1 or > MAX_INSERTION_SORT_THRESHOLD (1000). The threshold controls when SpreadSort switches to insertion sort for small segments; values outside [1, 1000] defeat this optimization. The message interpolates the upper bound constant.","triggerScenarios":"new SpreadSort(0, cap, buckets); new SpreadSort(1001, cap, buckets); new SpreadSort(-5, cap, buckets); loading the threshold from config without bounds checking.","commonSituations":"Config typo leaving the value at 0; formula computing a threshold that exceeds 1000 for large inputs; exposing the threshold to users without input validation.","solutions":["Validate 1 <= insertionSortThreshold <= 1000 before constructing.","Clamp config-derived values into the valid range with a sensible default.","Document the [1, 1000] range in your config schema and validate at parse time."],"exampleFix":"// before\nSpreadSort s = new SpreadSort(threshold, cap, buckets);\n\n// after\nint t = Math.max(1, Math.min(1000, threshold));\nSpreadSort s = new SpreadSort(t, cap, buckets);","handlingStrategy":"validation","validationCode":"if (insertionSortThreshold < 1 || insertionSortThreshold > 1000) throw new IllegalArgumentException(\"insertionSortThreshold must be in [1,1000]\");","typeGuard":"public static boolean isValidInsertionThreshold(int v) { return v >= 1 && v <= 1000; }","tryCatchPattern":null,"preventionTips":["Validate config-derived thresholds at parse time against [1, 1000].","Clamp to a sensible default (e.g. 16) when out of range.","Document the allowed range in your config schema."],"tags":["sorting","config","range-validation","constructor"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}