ronantakizawa/codereview-bench huggingface dataset:
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/sorts/BeadSortTest.java (Java)

Code before:
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class BeadSortTest {
    // BeadSort can't sort negative number, Character, String. It can sort positive number only

    @ParameterizedTest
    @MethodSource("provideArraysForBeadSort")
    public void testBeadSort(int[] inputArray, int[] expectedArray) {
        BeadSort beadSort = new BeadSort();
        assertArrayEquals(expectedArray, beadSort.sort(inputArray));
    }

    private static Stream<Arguments> provideArraysForBeadSort() {
        return Stream.of(Arguments.of(new int[] {}, new int[] {}), Arguments.of(new int[] {4}, new int[] {4}), Arguments.of(new int[] {6, 1, 99, 27, 15, 23, 36}, new int[] {1, 6, 15, 23, 27, 36, 99}), Arguments.of(new int[] {6, 1, 27, 15, 23, 27, 36, 23}, new int[] {1, 6, 15, 23, 23, 27, 27, 36}),
            Arguments.of(new int[] {5, 5, 5, 5, 5}, new int[] {5, 5, 5, 5, 5}), Arguments.of(new int[] {1, 2, 3, 4, 5}, new int[] {1, 2, 3, 4, 5}), Arguments.of(new int[] {5, 4, 3, 2, 1}, new int[] {1, 2, 3, 4, 5}));
    }

    @Test
    public void testWithNegativeNumbers() {
        assertThrows(IllegalArgumentException.class, () -> new BeadSort().sort(new int[] {3, -1, 4, 1, 5, -9}));
    }
}


Review comment (suggestion):
Single negative number is _harder to find_.

```suggestion
        assertThrows(IllegalArgumentException.class, () -> new BeadSort().sort(new int[] {3, 1, 4, 1, 5, -9}));
```

Code after:
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class BeadSortTest {
    @ParameterizedTest
    @MethodSource("provideArraysForBeadSort")
    public void testBeadSort(int[] inputArray, int[] expectedArray) {
        BeadSort beadSort = new BeadSort();
        assertArrayEquals(expectedArray, beadSort.sort(inputArray));
    }

    private static Stream<Arguments> provideArraysForBeadSort() {
        return Stream.of(Arguments.of(new int[] {}, new int[] {}), Arguments.of(new int[] {4}, new int[] {4}), Arguments.of(new int[] {6, 1, 99, 27, 15, 23, 36}, new int[] {1, 6, 15, 23, 27, 36, 99}), Arguments.of(new int[] {6, 1, 27, 15, 23, 27, 36, 23}, new int[] {1, 6, 15, 23, 23, 27, 27, 36}),
            Arguments.of(new int[] {5, 5, 5, 5, 5}, new int[] {5, 5, 5, 5, 5}), Arguments.of(new int[] {1, 2, 3, 4, 5}, new int[] {1, 2, 3, 4, 5}), Arguments.of(new int[] {5, 4, 3, 2, 1}, new int[] {1, 2, 3, 4, 5}));
    }

    @Test
    public void testWithNegativeNumbers() {
        assertThrows(IllegalArgumentException.class, () -> new BeadSort().sort(new int[] {3, 1, 4, 1, 5, -9}));
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/sorts/RadixSort.java (Java)

Code before:
     *
     * @param array the array to be sorted
     * @return the sorted array
     */
    public static int[] sort(int[] array) {
        if (array.length == 0) {
            return array;
        }

        final int max = Arrays.stream(array).max().getAsInt();
        for (int exp = 1; max / exp > 0; exp *= 10) {
            countingSortByDigit(array, exp);
        }

        return array;
    }

    /**
     * A utility method to perform counting sort of array[] according to the digit represented by exp.
     *
     * @param array the array to be sorted
     * @param exp   the exponent representing the current digit position
     */
    private static void countingSortByDigit(int[] array, int exp) {
        int[] output = new int[array.length];
        int[] count = new int[10];

        for (int i = 0; i < array.length; i++) {
            count[(array[i] / exp) % 10]++;
        }

        for (int i = 1; i < 10; i++) {
            count[i] += count[i - 1];
        }

        for (int i = array.length - 1; i >= 0; i--) {
            int digit = (array[i] / exp) % 10;
            output[count[digit] - 1] = array[i];
            count[digit]--;
        }

        System.arraycopy(output, 0, array, 0, array.length);
    }
}
// Written by James Mc Dermott(theycallmemac)


Review comment (suggestion):
What do you think about introducing
```java
private static final int BASE = 10;
```

Code after:

    /**
     * Sorts an array of nonnegative integers using the radix sort algorithm.
     *
     * @param array the array to be sorted
     * @return the sorted array
     * @throws IllegalArgumentException if any negative integers are found
     */
    public static int[] sort(int[] array) {
        if (array.length == 0) {
            return array;
        }

        checkForNegativeInput(array);
        radixSort(array);
        return array;
    }

    /**
     * Checks if the array contains any negative integers.
     *
     * @param array the array to be checked
     * @throws IllegalArgumentException if any negative integers are found
     */
    private static void checkForNegativeInput(int[] array) {
        for (int number : array) {
            if (number < 0) {
                throw new IllegalArgumentException("Array contains non-positive integers.");
            }
        }
    }

    private static void radixSort(int[] array) {
        final int max = Arrays.stream(array).max().getAsInt();
        for (int i = 0, exp = 1; i < NumberOfDigits.numberOfDigits(max); i++, exp *= BASE) {
            countingSortByDigit(array, exp);
        }
    }

    /**
     * A utility method to perform counting sort of array[] according to the digit represented by exp.
     *
     * @param array the array to be sorted
     * @param exp   the exponent representing the current digit position
     */
    private static void countingSortByDigit(int[] array, int exp) {
        int[] count = countDigits(array, exp);
        accumulateCounts(count);
        int[] output = buildOutput(array, exp, count);
        copyOutput(array, output);
    }
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/sorts/RadixSort.java (Java)

Code before:
        buildOutput(array, exp, output, count);
        copyOutput(array, output);
    }

    private static void countDigits(int[] array, int exp, int[] count) {
        for (int i = 0; i < array.length; i++) {
            count[getDigit(array[i], exp)]++;
        }
    }

    private static int getDigit(int number, int position) {
        return (number / position) % BASE;
    }

    private static void accumulateCounts(int[] count) {
        for (int i = 1; i < BASE; i++) {
            count[i] += count[i - 1];
        }
    }

    private static void buildOutput(int[] array, int exp, int[] output, int[] count) {
        for (int i = array.length - 1; i >= 0; i--) {
            int digit = getDigit(array[i], exp);
            output[count[digit] - 1] = array[i];
            count[digit]--;
        }
    }

    private static void copyOutput(int[] array, int[] output) {
        System.arraycopy(output, 0, array, 0, array.length);
    }
}


Review comment (suggestion):
```suggestion
    private static void buildOutput(int[] array, int exp, int[] count) {
        int[] output = new int[array.length];
        for (int i = array.length - 1; i >= 0; i--) {
            int digit = getDigit(array[i], exp);
            output[count[digit] - 1] = array[i];
            count[digit]--;
        }
        return output;
```

Code after:

    private static int[] countDigits(int[] array, int exp) {
        int[] count = new int[BASE];
        for (int i = 0; i < array.length; i++) {
            count[getDigit(array[i], exp)]++;
        }
        return count;
    }

    private static int getDigit(int number, int position) {
        return (number / position) % BASE;
    }

    private static void accumulateCounts(int[] count) {
        for (int i = 1; i < BASE; i++) {
            count[i] += count[i - 1];
        }
    }

    private static int[] buildOutput(int[] array, int exp, int[] count) {
        int[] output = new int[array.length];
        for (int i = array.length - 1; i >= 0; i--) {
            int digit = getDigit(array[i], exp);
            output[count[digit] - 1] = array[i];
            count[digit]--;
        }
        return output;
    }

    private static void copyOutput(int[] array, int[] output) {
        System.arraycopy(output, 0, array, 0, array.length);
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/sorts/RadixSort.java (Java)

Code before:
package com.thealgorithms.sorts;

import com.thealgorithms.maths.NumberOfDigits;
import java.util.Arrays;

/**
 * This class provides an implementation of the radix sort algorithm.
 * It sorts an array of positive integers in increasing order.
 */
public final class RadixSort {
    private static final int BASE = 10;

    private RadixSort() {
    }

    /**
     * Sorts an array of positive integers using the radix sort algorithm.
     *
     * @param array the array to be sorted
     * @return the sorted array
     * @throws IllegalArgumentException if any negative integers are found
     */
    public static int[] sort(int[] array) {
        if (array.length == 0) {
            return array;
        }

        checkForNegativeInput(array);
        radixSort(array);
        return array;
    }

    /**
     * Checks if the array contains any negative integers.
     *
     * @param array the array to be checked
     * @throws IllegalArgumentException if any negative integers are found
     */
    private static void checkForNegativeInput(int[] array) {
        for (int number : array) {
            if (number < 0) {
                throw new IllegalArgumentException("Array contains non-positive integers.");

Review comment (suggestion):
```suggestion
     * Sorts an array of nonnegative integers using the radix sort algorithm.
```

Code after:
package com.thealgorithms.sorts;

import com.thealgorithms.maths.NumberOfDigits;
import java.util.Arrays;

/**
 * This class provides an implementation of the radix sort algorithm.
 * It sorts an array of nonnegative integers in increasing order.
 */
public final class RadixSort {
    private static final int BASE = 10;

    private RadixSort() {
    }

    /**
     * Sorts an array of nonnegative integers using the radix sort algorithm.
     *
     * @param array the array to be sorted
     * @return the sorted array
     * @throws IllegalArgumentException if any negative integers are found
     */
    public static int[] sort(int[] array) {
        if (array.length == 0) {
            return array;
        }

        checkForNegativeInput(array);
        radixSort(array);
        return array;
    }

    /**
     * Checks if the array contains any negative integers.
     *
     * @param array the array to be checked
     * @throws IllegalArgumentException if any negative integers are found
     */
    private static void checkForNegativeInput(int[] array) {
        for (int number : array) {
            if (number < 0) {
                throw new IllegalArgumentException("Array contains non-positive integers.");
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/sorts/CountingSort.java (Java)

Code before:
package com.thealgorithms.sorts;

import java.util.Arrays;

/**
 * A standard implementation of the Counting Sort algorithm for integer arrays.
 * This implementation has a time complexity of O(n + k), where n is the number
 * of elements in the input array and k is the range of the input.
 * It works only with integer arrays.
 *
 * The space complexity is O(k), where k is the range of the input integers.
 *
 * Note: This implementation does not handle negative integers as it
 * calculates the range based on the minimum and maximum values of the array.
 *
 */
public final class CountingSort {
    private CountingSort() {
    }

    /**
     * Sorts an array of integers using the Counting Sort algorithm.
     *
     * @param array the array to be sorted
     * @return the sorted array
     */
    public static int[] sort(int[] array) {
        if (array.length == 0) {
            return array;
        }

        final int max = Arrays.stream(array).max().orElse(Integer.MIN_VALUE);
        final int min = Arrays.stream(array).min().orElse(Integer.MAX_VALUE);
        final int range = max - min + 1;

        int[] count = new int[range];
        int[] output = new int[array.length];

        for (int value : array) {

Review comment (suggestion):
I think your implementation indeed works for negative numbers.

By the way: to inform the user by throwing an exception for example.

Code after:
package com.thealgorithms.sorts;

import java.util.Arrays;

/**
 * A standard implementation of the Counting Sort algorithm for integer arrays.
 * This implementation has a time complexity of O(n + k), where n is the number
 * of elements in the input array and k is the range of the input.
 * It works only with integer arrays.
 *
 * The space complexity is O(k), where k is the range of the input integers.
 *
 * Note: This implementation handles negative integers as it
 * calculates the range based on the minimum and maximum values of the array.
 *
 */
public final class CountingSort {
    private CountingSort() {
    }

    /**
     * Sorts an array of integers using the Counting Sort algorithm.
     *
     * @param array the array to be sorted
     * @return the sorted array
     */
    public static int[] sort(int[] array) {
        if (array.length == 0) {
            return array;
        }
        final var stats = Arrays.stream(array).summaryStatistics();
        final int min = stats.getMin();
        int[] count = computeHistogram(array, min, stats.getMax() - min + 1);
        toCumulative(count);
        return reconstructSorted(count, min, array);
    }

    private static int[] computeHistogram(final int[] array, final int shift, final int spread) {
        int[] res = new int[spread];
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/sorts/CountingSort.java (Java)

Code before:
        if (array.length == 0) {
            return array;
        }

        final int max = Arrays.stream(array).max().orElse(Integer.MIN_VALUE);
        final int min = Arrays.stream(array).min().orElse(Integer.MAX_VALUE);
        final int range = max - min + 1;

        int[] count = new int[range];
        int[] output = new int[array.length];

        for (int value : array) {
            count[value - min]++;
        }

        for (int i = 1; i < count.length; i++) {
            count[i] += count[i - 1];
        }

        for (int i = array.length - 1; i >= 0; i--) {
            output[count[array[i] - min] - 1] = array[i];
            count[array[i] - min]--;
        }

        return output;
    }
}


Review comment (suggestion):
```suggestion
    public static int[] sort(int[] array) {
        if (array.length == 0) {
            return array;
        }

        final var stats = Arrays.stream(array).summaryStatistics();
        final int min = stats.getMin();

        int[] count = computeHistogram(array, min, stats.getMax() - min + 1);
        toCumulative(count);
        return reconstructSorted(count, min, array);
    }

    private static int[] computeHistogram(final int[] array, final int shift, final int spread) {
        int[] res = new int[spread];
        for (final var value : array) {
            res[value - shift]++;
        }
        return res;
    }

    private static void toCumulative(int[] count) {
        for (int i = 1; i < count.length; i++) {
            count[i] += count[i - 1];
        }
    }

    private static int[] reconstructSorted(final int[] cumulativeCount, final int shift, final int[] array) {
        int[] res = new int[array.length];
        for (int i = array.length - 1; i >= 0; i--) {
            res[cumulativeCount[array[i] - shift] - 1] = array[i];
            cumulativeCount[array[i] - shift]--;
        }

        return res;
    }
```

Code after:
        if (array.length == 0) {
            return array;
        }
        final var stats = Arrays.stream(array).summaryStatistics();
        final int min = stats.getMin();
        int[] count = computeHistogram(array, min, stats.getMax() - min + 1);
        toCumulative(count);
        return reconstructSorted(count, min, array);
    }

    private static int[] computeHistogram(final int[] array, final int shift, final int spread) {
        int[] res = new int[spread];
        for (final var value : array) {
            res[value - shift]++;
        }
        return res;
    }

    private static void toCumulative(int[] count) {
        for (int i = 1; i < count.length; i++) {
            count[i] += count[i - 1];
        }
    }

    private static int[] reconstructSorted(final int[] cumulativeCount, final int shift, final int[] array) {
        int[] res = new int[array.length];
        for (int i = array.length - 1; i >= 0; i--) {
            res[cumulativeCount[array[i] - shift] - 1] = array[i];
            cumulativeCount[array[i] - shift]--;
        }
        return res;
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/sorts/CountingSort.java (Java)

Code before:
package com.thealgorithms.sorts;

import java.util.Arrays;

/**
 * A standard implementation of the Counting Sort algorithm for integer arrays.
 * This implementation has a time complexity of O(n + k), where n is the number
 * of elements in the input array and k is the range of the input.
 * It works only with integer arrays.
 *
 * The space complexity is O(k), where k is the range of the input integers.
 *
 * Note: This implementation handle negative integers as it
 * calculates the range based on the minimum and maximum values of the array.
 *
 */
public final class CountingSort {
    private CountingSort() {
    }

    /**
     * Sorts an array of integers using the Counting Sort algorithm.
     *
     * @param array the array to be sorted
     * @return the sorted array
     */
    public static int[] sort(int[] array) {
        if (array.length == 0) {
            return array;
        }
        final var stats = Arrays.stream(array).summaryStatistics();
        final int min = stats.getMin();
        int[] count = computeHistogram(array, min, stats.getMax() - min + 1);
        toCumulative(count);
        return reconstructSorted(count, min, array);
    }

    private static int[] computeHistogram(final int[] array, final int shift, final int spread) {

Review comment (suggestion):
```suggestion
 * Note: This implementation handles negative integers as it
```

Code after:
package com.thealgorithms.sorts;

import java.util.Arrays;

/**
 * A standard implementation of the Counting Sort algorithm for integer arrays.
 * This implementation has a time complexity of O(n + k), where n is the number
 * of elements in the input array and k is the range of the input.
 * It works only with integer arrays.
 *
 * The space complexity is O(k), where k is the range of the input integers.
 *
 * Note: This implementation handles negative integers as it
 * calculates the range based on the minimum and maximum values of the array.
 *
 */
public final class CountingSort {
    private CountingSort() {
    }

    /**
     * Sorts an array of integers using the Counting Sort algorithm.
     *
     * @param array the array to be sorted
     * @return the sorted array
     */
    public static int[] sort(int[] array) {
        if (array.length == 0) {
            return array;
        }
        final var stats = Arrays.stream(array).summaryStatistics();
        final int min = stats.getMin();
        int[] count = computeHistogram(array, min, stats.getMax() - min + 1);
        toCumulative(count);
        return reconstructSorted(count, min, array);
    }

    private static int[] computeHistogram(final int[] array, final int shift, final int spread) {
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/sorts/PigeonholeSort.java (Java)

Code before:
package com.thealgorithms.sorts;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public final class PigeonholeSort {
    private PigeonholeSort() {
    }

    /**
     * Sorts the given array using the pigeonhole sort algorithm.
     *
     * @param array the array to be sorted
     * @throws IllegalArgumentException if any negative integers are found
     * @return the sorted array
     */
    public static int[] sort(int[] array) {
        if (array.length < 2) {
            return array;
        }

        checkForNegativeInput(array);

        final int maxElement = Arrays.stream(array).max().orElseThrow();
        final List<List<Integer>> pigeonHoles = createPigeonHoles(maxElement);

        populatePigeonHoles(array, pigeonHoles);
        collectFromPigeonHoles(array, pigeonHoles);

        return array;
    }

    /**
     * Checks if the array contains any negative integers.
     *
     * @param array the array to be checked
     * @throws IllegalArgumentException if any negative integers are found
     */
    private static void checkForNegativeInput(int[] array) {
        for (int number : array) {
            if (number < 0) {
                throw new IllegalArgumentException("Array contains non-positive integers.");
            }
        }

Review comment (suggestion):
```suggestion
        checkForNegativeInput(array);

        if (array.length == 0) {
            return array;
```

Two things:
- the check for negative numbers has to happen first,
- it seems enough to check if the array is empty.

Code after:
package com.thealgorithms.sorts;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public final class PigeonholeSort {
    private PigeonholeSort() {
    }

    /**
     * Sorts the given array using the pigeonhole sort algorithm.
     *
     * @param array the array to be sorted
     * @throws IllegalArgumentException if any negative integers are found
     * @return the sorted array
     */
    public static int[] sort(int[] array) {

        checkForNegativeInput(array);

        if (array.length == 0) {
            return array;
        }

        final int maxElement = Arrays.stream(array).max().orElseThrow();
        final List<List<Integer>> pigeonHoles = createPigeonHoles(maxElement);

        populatePigeonHoles(array, pigeonHoles);
        collectFromPigeonHoles(array, pigeonHoles);

        return array;
    }

    /**
     * Checks if the array contains any negative integers.
     *
     * @param array the array to be checked
     * @throws IllegalArgumentException if any negative integers are found
     */
    private static void checkForNegativeInput(int[] array) {
        for (final int number : array) {
            if (number < 0) {
                throw new IllegalArgumentException("Array contains negative integers.");
            }