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

Code before:
     * @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) {
        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 (array == null || array.length == 0) {
            return;
        }
        if (index == array.length - 1) {
            return;
        }

        // Find the minimum element in the remaining unsorted array
        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);
        }

        // 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) {
        int currentMinIndex = start;

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

Review comment (suggestion):
```suggestion
        final int minIndex = findMinIndex(array, index);
```

Code after:
     * @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);
        }

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