TheAlgorithms/Java · error · IllegalArgumentException

Initial bucket capacity must be between 1 and 1000

Error message

Initial bucket capacity must be between 1 and 1000

What it means

Thrown by the SpreadSort(int, int, int) constructor when initialBucketCapacity < 1 or > MAX_INITIAL_BUCKET_CAPACITY (1000). The capacity pre-allocates each bucket; values outside [1, 1000] cause either zero-capacity buckets (immediate resize churn) or excessive memory. The message interpolates the upper bound constant.

Source

Thrown at src/main/java/com/thealgorithms/sorts/SpreadSort.java:31

    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

  1. Validate 1 <= initialBucketCapacity <= 1000 before constructing.
  2. Clamp config-derived values into the valid range with a sensible default.
  3. Validate at config parse time and surface a clear error listing the allowed range.

Example fix

// before
SpreadSort s = new SpreadSort(threshold, capacity, buckets);

// after
int c = Math.max(1, Math.min(1000, capacity));
SpreadSort s = new SpreadSort(threshold, c, buckets);
Defensive patterns

Strategy: validation

Validate before calling

if (initialBucketCapacity < 1 || initialBucketCapacity > 1000) throw new IllegalArgumentException("initialBucketCapacity must be in [1,1000]");

Type guard

public static boolean isValidBucketCapacity(int v) { return v >= 1 && v <= 1000; }

Prevention

When it happens

Trigger: new SpreadSort(threshold, 0, buckets); new SpreadSort(threshold, 1001, buckets); new SpreadSort(threshold, -1, buckets); loading capacity from config without bounds checking.

Common situations: Config default left at 0; capacity derived from an input-size formula that can exceed 1000; user-tunable knob without validation.

Related errors


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