TheAlgorithms/Java · error · IllegalArgumentException
Insertion sort threshold must be between 1 and 1000
Error message
Insertion sort threshold must be between 1 and 1000
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/sorts/SpreadSort.java:28
public class SpreadSort implements SortAlgorithm {
private static final int MAX_INSERTION_SORT_THRESHOLD = 1000;
private static final int MAX_INITIAL_BUCKET_CAPACITY = 1000;
private static final int MAX_MIN_BUCKETS = 100;
private final int insertionSortThreshold;
private final int initialBucketCapacity;
private final int minBuckets;
/**
* Constructor to initialize the SpreadSort algorithm with custom parameters.
*
* @param insertionSortThreshold the threshold for using insertion sort for small segments (1-1000)
* @param initialBucketCapacity the initial capacity for each bucket (1-1000)
* @param minBuckets the minimum number of buckets to use (1-100)
*/
public SpreadSort(int insertionSortThreshold, int initialBucketCapacity, int minBuckets) {
if (insertionSortThreshold < 1 || insertionSortThreshold > MAX_INSERTION_SORT_THRESHOLD) {
throw new IllegalArgumentException("Insertion sort threshold must be between 1 and " + MAX_INSERTION_SORT_THRESHOLD);
}
if (initialBucketCapacity < 1 || initialBucketCapacity > MAX_INITIAL_BUCKET_CAPACITY) {
throw new IllegalArgumentException("Initial bucket capacity must be between 1 and " + MAX_INITIAL_BUCKET_CAPACITY);
}
if (minBuckets < 1 || minBuckets > MAX_MIN_BUCKETS) {
throw new IllegalArgumentException("Minimum number of buckets must be between 1 and " + MAX_MIN_BUCKETS);
}
this.insertionSortThreshold = insertionSortThreshold;
this.initialBucketCapacity = initialBucketCapacity;
this.minBuckets = minBuckets;
}
/**
* Default constructor with predefined values.
*/
public SpreadSort() {
this(16, 16, 2);View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before SpreadSort s = new SpreadSort(threshold, cap, buckets); // after int t = Math.max(1, Math.min(1000, threshold)); SpreadSort s = new SpreadSort(t, cap, buckets);
Defensive patterns
Strategy: validation
Validate before calling
if (insertionSortThreshold < 1 || insertionSortThreshold > 1000) throw new IllegalArgumentException("insertionSortThreshold must be in [1,1000]"); Type guard
public static boolean isValidInsertionThreshold(int v) { return v >= 1 && v <= 1000; } Prevention
- 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.
When it happens
Trigger: new SpreadSort(0, cap, buckets); new SpreadSort(1001, cap, buckets); new SpreadSort(-5, cap, buckets); loading the threshold from config without bounds checking.
Common situations: 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.
Related errors
- Classification ratio must be between 0 and 1 (exclusive).
- Initial bucket capacity must be between 1 and 1000
- Minimum number of buckets must be between 1 and 100
- Alpha must be between 0 and 1.
- order must be greater than zero
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/3c308992ac70541d.
Report an issue: GitHub.