TheAlgorithms/Java · error · IllegalArgumentException

Minimum number of buckets must be between 1 and 100

Error message

Minimum number of buckets must be between 1 and 100

What it means

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.

Source

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

    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);
    }

    /**
     * Sorts an array using the SpreadSort algorithm.
     *
     * @param array the array to be sorted

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass a minBuckets value within 1..100 (the no-arg `new SpreadSort()` uses 2).
  2. If computing minBuckets from input size, clamp it: `Math.min(100, Math.max(1, computed))`.
  3. Verify the three constants MAX_INSERTION_SORT_THRESHOLD, MAX_INITIAL_BUCKET_CAPACITY, MAX_MIN_BUCKETS match the ranges you assume before constructing.

Example fix

// before
new SpreadSort(16, 16, 0);   // throws: minBuckets out of range

// after
new SpreadSort(16, 16, Math.min(100, Math.max(1, n / 10)));
// or simply
new SpreadSort();
Defensive patterns

Strategy: validation

Validate before calling

private static final int MAX_MIN_BUCKETS = 100;
static SpreadSort safeSort(int threshold, int capacity, int minBuckets) {
    if (minBuckets < 1 || minBuckets > MAX_MIN_BUCKETS) {
        throw new IllegalArgumentException(
            "minBuckets " + minBuckets + " out of range [1, " + MAX_MIN_BUCKETS + "]");
    }
    return new SpreadSort(threshold, capacity, minBuckets);
}

Type guard

static boolean isValidMinBuckets(int minBuckets) {
    return minBuckets >= 1 && minBuckets <= 100;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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