TheAlgorithms/Java · error · IllegalArgumentException

Classification ratio must be between 0 and 1 (exclusive).

Error message

Classification ratio must be between 0 and 1 (exclusive).

What it means

Thrown by the FlashSort(double) constructor when classificationRatio <= 0 or >= 1. The classification ratio positions each element into a bucket based on its value relative to the min/max range; values of 0 or 1 collapse the classification into a single bucket and break the algorithm. Both the constructor and setter enforce the same (0,1) open interval.

Source

Thrown at src/main/java/com/thealgorithms/sorts/FlashSort.java:32

 * <ol>
 *     <li>Finds the minimum and maximum values in the array.</li>
 *     <li>Initializes a classification array `L` to keep track of the number of elements in each class.</li>
 *     <li>Computes a normalization constant `c1` to map elements into classes.</li>
 *     <li>Classifies each element of the array into the corresponding bucket in the classification array.</li>
 *     <li>Transforms the classification array to compute the starting indices of each bucket.</li>
 *     <li>Permutes the elements of the array into sorted order based on the classification.</li>
 *     <li>Uses insertion sort for the final arrangement to ensure complete sorting.</li>
 * </ol>
 */
public class FlashSort implements SortAlgorithm {
    private double classificationRatio = 0.45;

    public FlashSort() {
    }

    public FlashSort(double classificationRatio) {
        if (classificationRatio <= 0 || classificationRatio >= 1) {
            throw new IllegalArgumentException("Classification ratio must be between 0 and 1 (exclusive).");
        }
        this.classificationRatio = classificationRatio;
    }

    public double getClassificationRatio() {
        return classificationRatio;
    }

    public void setClassificationRatio(double classificationRatio) {
        if (classificationRatio <= 0 || classificationRatio >= 1) {
            throw new IllegalArgumentException("Classification ratio must be between 0 and 1 (exclusive).");
        }
        this.classificationRatio = classificationRatio;
    }

    /**
     * Sorts an array using the Flash Sort algorithm.
     *

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate 0 < ratio < 1 before constructing or calling the setter.
  2. Clamp config-derived ratios to a safe default (e.g. 0.45) when out of range.
  3. Document the open interval (0,1) in your config schema and validate at parse time.

Example fix

// before
FlashSort s = new FlashSort(ratio);

// after
double r = (ratio <= 0 || ratio >= 1) ? 0.45 : ratio;
FlashSort s = new FlashSort(r);
Defensive patterns

Strategy: validation

Validate before calling

if (classificationRatio <= 0 || classificationRatio >= 1) throw new IllegalArgumentException("ratio must be in (0,1)");

Type guard

public static boolean isValidRatio(double r) { return r > 0 && r < 1; }

Prevention

When it happens

Trigger: new FlashSort(0.0); new FlashSort(1.0); new FlashSort(1.5); new FlashSort(-0.2); calling setClassificationRatio with the same out-of-range values.

Common situations: Loading the ratio from config with a typo or missing value defaulting to 0; computing the ratio from a formula that can equal 0 or 1 at boundary inputs; exposing it as a user-tunable knob without validation.

Related errors


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