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

Code before:
package com.thealgorithms.sorts;

import java.util.Arrays;

/**
 * BitonicSort class implements the SortAlgorithm interface using the bitonic sort technique.
 */
public class BitonicSort implements SortAlgorithm {

    /**
     * Sorts the given array using the Bitonic Sort algorithm.
     *
     * @param <T> the type of elements in the array, which must implement the Comparable interface
     * @param arr the array to be sorted
     * @return the sorted array
     */
    @Override
    public <T extends Comparable<T>> T[] sort(T[] arr) {
        if (arr == null || arr.length == 0) {
            return arr;
        }

        final int paddedSize = nextPowerOfTwo(arr.length);
        T[] paddedArray = Arrays.copyOf(arr, paddedSize);

        // Fill the padded part with a maximum value
        final T maxValue = SortUtils.max(arr);
        Arrays.fill(paddedArray, arr.length, paddedSize, maxValue);

        bitonicSort(paddedArray, 0, paddedSize, true);
        return Arrays.copyOf(paddedArray, arr.length);
    }

    private <T extends Comparable<T>> void bitonicSort(T[] arr, int low, int cnt, boolean dir) {
        if (cnt > 1) {
            final int k = cnt / 2;

            // Sort first half in ascending order
            bitonicSort(arr, low, k, true);

            // Sort second half in descending order
            bitonicSort(arr, low + k, cnt - k, false);

            // Merge the whole sequence in ascending order

Review comment (suggestion):
As far as I see the sorts derived from `SortAlgorithm` assume that the input is not `null`. I think it is better to be consistent here.

Code after:
package com.thealgorithms.sorts;

import java.util.Arrays;
import java.util.function.BiPredicate;

/**
 * BitonicSort class implements the SortAlgorithm interface using the bitonic sort technique.
 */
public class BitonicSort implements SortAlgorithm {
    private enum Direction {
        DESCENDING,
        ASCENDING,
    }

    /**
     * Sorts the given array using the Bitonic Sort algorithm.
     *
     * @param <T> the type of elements in the array, which must implement the Comparable interface
     * @param array the array to be sorted
     * @return the sorted array
     */
    @Override
    public <T extends Comparable<T>> T[] sort(T[] array) {
        if (array.length == 0) {
            return array;
        }

        final int paddedSize = nextPowerOfTwo(array.length);
        T[] paddedArray = Arrays.copyOf(array, paddedSize);

        // Fill the padded part with a maximum value
        final T maxValue = max(array);
        Arrays.fill(paddedArray, array.length, paddedSize, maxValue);

        bitonicSort(paddedArray, 0, paddedSize, Direction.ASCENDING);
        return Arrays.copyOf(paddedArray, array.length);
    }

    private <T extends Comparable<T>> void bitonicSort(final T[] array, final int low, final int cnt, final Direction direction) {
        if (cnt > 1) {
            final int k = cnt / 2;

            // Sort first half in ascending order
            bitonicSort(array, low, k, Direction.ASCENDING);
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/sorts/WaveSort.java (Java)

Code before:
package com.thealgorithms.sorts;

/**
 * The WaveSort algorithm sorts an array so that every alternate element is greater than its adjacent elements.
 */
public class WaveSort implements SortAlgorithm {

    @Override
    public <T extends Comparable<T>> T[] sort(T[] array) {
        int n = array.length;

        for (int i = 0; i < n; i += 2) {
            if (i > 0 && SortUtils.less(array[i], array[i - 1])) {
                SortUtils.swap(array, i, i - 1);
            }

            if (i < n - 1 && SortUtils.less(array[i], array[i + 1])) {
                SortUtils.swap(array, i, i + 1);
            }
        }

        return array;
    }
}


Review comment (suggestion):
I would not derive the `WaveSort` from `SortAlgorithm`. Each class implementing `SortAlgorithm` should be tested by a class derived from `SortingAlgorithmTest` - in this case it will simply not work. Furthermore `WaveSort` is so much different that the usual _sorting algorithm_ (e.g. the output is not unique).

Code after:
package com.thealgorithms.sorts;

/**
 * The WaveSort algorithm sorts an array so that every alternate element is greater than its adjacent elements.
 * This implementation also provides a method to check if an array is wave sorted.
 */
public class WaveSort implements SortAlgorithm {
    /**
     * Sorts the given array such that every alternate element is greater than its adjacent elements.
     *
     * @param array The array to be sorted.
     * @param <T> The type of elements in the array, which must be Comparable.
     * @return The sorted array.
     */
    @Override
    public <T extends Comparable<T>> T[] sort(T[] array) {
        for (int i = 0; i < array.length; i += 2) {
            if (i > 0 && SortUtils.less(array[i], array[i - 1])) {
                SortUtils.swap(array, i, i - 1);
            }
            if (i < array.length - 1 && SortUtils.less(array[i], array[i + 1])) {
                SortUtils.swap(array, i, i + 1);
            }
        }
        return array;
    }

    /**
     * Checks if the given array is wave sorted. An array is wave sorted if every alternate element is greater than its adjacent elements.
     *
     * @param array The array to check.
     * @param <T> The type of elements in the array, which must be Comparable.
     * @return true if the array is wave sorted, false otherwise.
     */
    public <T extends Comparable<T>> boolean isWaveSorted(T[] array) {
        for (int i = 0; i < array.length; i += 2) {
            if (i > 0 && SortUtils.less(array[i], array[i - 1])) {
                return false;
            }
            if (i < array.length - 1 && SortUtils.less(array[i], array[i + 1])) {
                return false;
            }
        }
        return true;
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/sorts/WaveSort.java (Java)

Code before:
package com.thealgorithms.sorts;

/**
 * The WaveSort algorithm sorts an array so that every alternate element is greater than its adjacent elements.
 */
public class WaveSort implements SortAlgorithm {
    @Override
    public <T extends Comparable<T>> T[] sort(T[] array) {
        int n = array.length;

        for (int i = 0; i < n; i += 2) {
            if (i > 0 && SortUtils.less(array[i], array[i - 1])) {
                SortUtils.swap(array, i, i - 1);
            }

            if (i < n - 1 && SortUtils.less(array[i], array[i + 1])) {
                SortUtils.swap(array, i, i + 1);
            }
        }

        return array;
    }

    public <T extends Comparable<T>> boolean isWaveSorted(T[] array) {
        for (int i = 0; i < array.length; i += 2) {
            if (i > 0 && SortUtils.less(array[i], array[i - 1])) {
                return false;
            }
            if (i < array.length - 1 && SortUtils.less(array[i], array[i + 1])) {
                return false;
            }
        }
        return true;
    }
}


Review comment (suggestion):
```suggestion
        for (int i = 0; i < array.length; i += 2) {
            if (i > 0 && SortUtils.less(array[i], array[i - 1])) {
                SortUtils.swap(array, i, i - 1);
            }

            if (i < array.length - 1 && SortUtils.less(array[i], array[i + 1])) {
```

Code after:
package com.thealgorithms.sorts;

/**
 * The WaveSort algorithm sorts an array so that every alternate element is greater than its adjacent elements.
 * This implementation also provides a method to check if an array is wave sorted.
 */
public class WaveSort implements SortAlgorithm {
    /**
     * Sorts the given array such that every alternate element is greater than its adjacent elements.
     *
     * @param array The array to be sorted.
     * @param <T> The type of elements in the array, which must be Comparable.
     * @return The sorted array.
     */
    @Override
    public <T extends Comparable<T>> T[] sort(T[] array) {
        for (int i = 0; i < array.length; i += 2) {
            if (i > 0 && SortUtils.less(array[i], array[i - 1])) {
                SortUtils.swap(array, i, i - 1);
            }
            if (i < array.length - 1 && SortUtils.less(array[i], array[i + 1])) {
                SortUtils.swap(array, i, i + 1);
            }
        }
        return array;
    }

    /**
     * Checks if the given array is wave sorted. An array is wave sorted if every alternate element is greater than its adjacent elements.
     *
     * @param array The array to check.
     * @param <T> The type of elements in the array, which must be Comparable.
     * @return true if the array is wave sorted, false otherwise.
     */
    public <T extends Comparable<T>> boolean isWaveSorted(T[] array) {
        for (int i = 0; i < array.length; i += 2) {
            if (i > 0 && SortUtils.less(array[i], array[i - 1])) {
                return false;
            }
            if (i < array.length - 1 && SortUtils.less(array[i], array[i + 1])) {
                return false;
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java (Java)

Code before:

        // Swap the found minimum element with the element at the current index
        if (minIndex != index) {
            SortUtils.swap(array, index, minIndex);
        }

        // Recursively call selection sort for the remaining array
        recursiveSelectionSort(array, index + 1);
    }

    /**
     * Finds the index of the minimum element in the array starting from the given index.
     *
     * @param array the array to search
     * @param start the starting index for the search
     * @param <T>   the type of elements in the array
     * @return the index of the minimum element
     */
    private static <T extends Comparable<T>> int findMinIndex(T[] array, int start) {
        // Base case: if start is the last index, return start
        if (start == array.length - 1) {
            return start;
        }

        // Recursive call to find the minimum index in the rest of the array
        int minIndexInRest = findMinIndex(array, start + 1);

        // Return the index of the smaller element between array[start] and the minimum element in the rest of the array
        return array[start].compareTo(array[minIndexInRest]) < 0 ? start : minIndexInRest;
    }
}


Review comment (suggestion):
```suggestion
        final int minIndexInRest = findMinIndex(array, start + 1);
```

Code after:

        // Swap the found minimum element with the element at the current index
        if (minIndex != index) {
            SortUtils.swap(array, index, minIndex);
        }

        // Recursively call selection sort for the remaining array
        recursiveSelectionSort(array, index + 1);
    }

    /**
     * Finds the index of the minimum element in the array starting from the given index.
     *
     * @param array the array to search in.
     * @param start the starting index.
     * @param <T> the type of the elements in the array, which must be Comparable.
     * @return the index of the minimum element starting from the given index.
     */
    private static <T extends Comparable<T>> int findMinIndex(T[] array, int start) {
        int currentMinIndex = start;

        for (int currentIndex = start + 1; currentIndex < array.length; currentIndex++) {
            if (array[currentIndex].compareTo(array[currentMinIndex]) < 0) {
                currentMinIndex = currentIndex;
            }
        }

        return currentMinIndex;
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/sorts/SelectionSortRecursive.java (Java)

Code before:
package com.thealgorithms.sorts;

/**
 * Class that implements the Selection Sort algorithm using recursion.
 */
public class SelectionSortRecursive implements SortAlgorithm {

    /**
     * Sorts an array using recursive selection sort.
     *
     * @param array the array to be sorted
     * @param <T>   the type of elements in the array (must be Comparable)
     * @return the sorted array
     */
    public <T extends Comparable<T>> T[] sort(T[] array) {
        if (array == null || array.length == 0) {
            return array;
        }
        recursiveSelectionSort(array, 0);
        return array;
    }

    /**
     * Recursively sorts the array using selection sort.
     *
     * @param array the array to be sorted
     * @param index the current index to start sorting from
     * @param <T>   the type of elements in the array (must be Comparable)
     */
    private static <T extends Comparable<T>> void recursiveSelectionSort(T[] array, int index) {
        if (index == array.length - 1) {
            return;
        }

        // Find the minimum element in the remaining unsorted array
        final int minIndex = findMinIndex(array, index);

        // Swap the found minimum element with the element at the current index
        if (minIndex != index) {
            SortUtils.swap(array, index, minIndex);
        }

Review comment (suggestion):
```suggestion
        if (array.length == 0) {
```

Code after:
package com.thealgorithms.sorts;

/**
 * Class that implements the Selection Sort algorithm using recursion.
 */
public class SelectionSortRecursive implements SortAlgorithm {

    /**
     * Sorts an array using recursive selection sort.
     *
     * @param array the array to be sorted
     * @param <T>   the type of elements in the array (must be Comparable)
     * @return the sorted array
     */
    public <T extends Comparable<T>> T[] sort(T[] array) {
        if (array.length == 0) {
            return array;
        }
        recursiveSelectionSort(array, 0);
        return array;
    }

    /**
     * Recursively sorts the array using selection sort.
     *
     * @param array the array to be sorted
     * @param index the current index to start sorting from
     * @param <T>   the type of elements in the array (must be Comparable)
     */
    private static <T extends Comparable<T>> void recursiveSelectionSort(T[] array, int index) {
        if (index == array.length - 1) {
            return;
        }

        // Find the minimum element in the remaining unsorted array
        final int minIndex = findMinIndex(array, index);

        // Swap the found minimum element with the element at the current index
        if (minIndex != index) {
            SortUtils.swap(array, index, minIndex);
        }
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/sorts/SortUtilsTest.java (Java)

Code before:
        assertFalse(SortUtils.isSorted(list));

        List<Integer> array2 = List.of(14, 15, 16, 1);
        assertFalse(SortUtils.isSorted(array2));

        List<Integer> array3 = List.of(5, 4, 3, 2, 1);
        assertFalse(SortUtils.isSorted(array3));
    }

    @ParameterizedTest
    @MethodSource("provideArraysForSwap")
    public <T> void testSwap(T[] array, int i, int j, T[] expected) {
        SortUtils.swap(array, i, j);
        assertArrayEquals(expected, array);
    }

    @ParameterizedTest
    @MethodSource("provideArraysForSwap")
    public <T> void testSwapFlippedIndices(T[] array, int i, int j, T[] expected) {
        SortUtils.swap(array, j, i);
        assertArrayEquals(expected, array);
    }

    private static Stream<Arguments> provideArraysForSwap() {
        return Stream.of(Arguments.of(new Integer[] {1, 2, 3, 4}, 1, 2, new Integer[] {1, 3, 2, 4}), Arguments.of(new Integer[] {1, 2, 3, 4}, 0, 3, new Integer[] {4, 2, 3, 1}), Arguments.of(new Integer[] {1, 2, 3, 4}, 2, 2, new Integer[] {1, 2, 3, 4}),
            Arguments.of(new String[] {"a", "b", "c", "d"}, 0, 3, new String[] {"d", "b", "c", "a"}), Arguments.of(new String[] {null, "b", "c", null}, 0, 3, new String[] {null, "b", "c", null}), Arguments.of(new Integer[] {}, 0, 0, new Integer[] {}));
    }
}


Review comment (suggestion):
In reality we should exclude such call.

```suggestion
            Arguments.of(new String[] {"a", "b", "c", "d"}, 0, 3, new String[] {"d", "b", "c", "a"}), Arguments.of(new String[] {null, "b", "c", null}, 0, 3, new String[] {null, "b", "c", null}));
```

Code after:
        assertFalse(SortUtils.isSorted(list));

        List<Integer> array2 = List.of(14, 15, 16, 1);
        assertFalse(SortUtils.isSorted(array2));

        List<Integer> array3 = List.of(5, 4, 3, 2, 1);
        assertFalse(SortUtils.isSorted(array3));
    }

    @ParameterizedTest
    @MethodSource("provideArraysForSwap")
    public <T> void testSwap(T[] array, int i, int j, T[] expected) {
        SortUtils.swap(array, i, j);
        assertArrayEquals(expected, array);
    }

    @ParameterizedTest
    @MethodSource("provideArraysForSwap")
    public <T> void testSwapFlippedIndices(T[] array, int i, int j, T[] expected) {
        SortUtils.swap(array, j, i);
        assertArrayEquals(expected, array);
    }

    private static Stream<Arguments> provideArraysForSwap() {
        return Stream.of(Arguments.of(new Integer[] {1, 2, 3, 4}, 1, 2, new Integer[] {1, 3, 2, 4}), Arguments.of(new Integer[] {1, 2, 3, 4}, 0, 3, new Integer[] {4, 2, 3, 1}), Arguments.of(new Integer[] {1, 2, 3, 4}, 2, 2, new Integer[] {1, 2, 3, 4}),
            Arguments.of(new String[] {"a", "b", "c", "d"}, 0, 3, new String[] {"d", "b", "c", "a"}), Arguments.of(new String[] {null, "b", "c", null}, 0, 3, new String[] {null, "b", "c", null}));
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/sorts/BubbleSortRecursive.java (Java)

Code before:
package com.thealgorithms.sorts;

/**
 * BubbleSort algorithm implements using recursion
 */
public class BubbleSortRecursive implements SortAlgorithm {
    /**
     * @param array - an array should be sorted
     * @return sorted array
     */
    @Override
    public <T extends Comparable<T>> T[] sort(T[] array) {
        bubbleSort(array, array.length);
        return array;
    }

    /**
     * BubbleSort algorithm implements using recursion
     *
     * @param array array contains elements
     * @param len length of given array
     */
    private static <T extends Comparable<T>> void bubbleSort(T[] array, int len) {
        boolean swapped = false;
        for (int i = 0; i < len - 1; ++i) {
            if (SortUtils.greater(array[i], array[i + 1])) {
                SortUtils.swap(array, i, i + 1);
                swapped = true;
            }

Review comment (suggestion):
```suggestion
 * BubbleSort algorithm implemented using recursion
```

Code after:
package com.thealgorithms.sorts;

/**
 * BubbleSort algorithm implemented using recursion
 */
public class BubbleSortRecursive implements SortAlgorithm {
    /**
     * @param array - an array should be sorted
     * @return sorted array
     */
    @Override
    public <T extends Comparable<T>> T[] sort(T[] array) {
        bubbleSort(array, array.length);
        return array;
    }

    /**
     * BubbleSort algorithm implements using recursion
     *
     * @param array array contains elements
     * @param len length of given array
     */
    private static <T extends Comparable<T>> void bubbleSort(T[] array, int len) {
        boolean swapped = false;
        for (int i = 0; i < len - 1; ++i) {
            if (SortUtils.greater(array[i], array[i + 1])) {
                SortUtils.swap(array, i, i + 1);
                swapped = true;
            }
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/sorts/BeadSort.java (Java)

Code before:
package com.thealgorithms.sorts;

import java.util.Arrays;

/**
 * BeadSort cannot sort negative numbers, characters, or strings.
 * It only works for non-negative integers.
 *
 * @see <a href="https://en.wikipedia.org/wiki/Bead_sort">BeadSort Algorithm (Wikipedia)</a>
 */
public class BeadSort {
    /**
     * Sorts the given array using the BeadSort algorithm.
     *
     * @param array The array of non-negative integers to be sorted.
     * @return The sorted array.
     * @throws IllegalArgumentException If the array contains negative numbers.
     */
    public int[] sort(int[] array) {
        if (array.length == 0) {
            return array;
        }

        if (Arrays.stream(array).anyMatch(s -> s < 0)) {
            throw new IllegalArgumentException("BeadSort cannot sort negative numbers.");
        }

        int[] sorted = new int[array.length];
        int max = Arrays.stream(array).max().orElse(0);

        char[][] grid = new char[array.length][max];
        int[] count = new int[max];

Review comment (suggestion):
```suggestion
 * BeadSort works for non-negative integers only.
```

Code after:
package com.thealgorithms.sorts;

import java.util.Arrays;

public class BeadSort {
    private enum BeadState { BEAD, EMPTY }

    /**
     * Sorts the given array using the BeadSort algorithm.
     *
     * @param array The array of non-negative integers to be sorted.
     * @return The sorted array.
     * @throws IllegalArgumentException If the array contains negative numbers.
     */
    public int[] sort(int[] array) {
        allInputsMustBeNonNegative(array);
        return extractSortedFromGrid(fillGrid(array));
    }

    private void allInputsMustBeNonNegative(final int[] array) {
        if (Arrays.stream(array).anyMatch(s -> s < 0)) {
            throw new IllegalArgumentException("BeadSort cannot sort negative numbers.");
        }
    }

    private BeadState[][] fillGrid(final int[] array) {
        final var maxValue = Arrays.stream(array).max().orElse(0);
        var grid = getEmptyGrid(array.length, maxValue);

        int[] count = new int[maxValue];
        for (int i = 0, arrayLength = array.length; i < arrayLength; i++) {
            int k = 0;