Selection and Bubble sorts Just to enjoy, I want to implement selection and bubble sorts algorithms without looking any implementation by my own efforts . Is there any redundant/lack part(s)? After improved their implementations, I think to try to write them down as recursive, can I ?, any hint?

class Sorts {

    public static void main(String[] args) {
        int[] arrSelection = { 8, 6, 3, 1 };
        selectionSort(arrSelection);
        for (int i : arrSelection ) {
            System.out.print(i + "  ");
        }

        System.out.print("\n");

        int[] arrBubble = { 8, 6, 3, 1 };
        bubbleSort(arrBubble);
        for (int i : arrBubble ) {
            System.out.print(i + "  ");
        }

    }

    // swap helper for java
    private static int returnFirst(int x, int y) {
        return x;
    }

    public static void selectionSort(int[] arr) {
        int lengthOfInputArray = arr.length;
        int min;
        int min_index;

        for (int i = 0; i &lt; lengthOfInputArray - 1; i++) {
            min = arr[i];
            min_index = i;
            for (int j = i+1; j &lt; lengthOfInputArray; j++) {
                if (min &gt; arr[j]) {
                    min = arr[j];
                    min_index = j;
                }
            }
            if (min_index != i) {
                // swap(arr[i], arr[min_index])
                arr[i] = returnFirst(arr[min_index], arr[min_index] = arr[i]);
            }
        }
    }

    public static void bubbleSort(int[] arr) {
        int lengthOfInputArray = arr.length;

        for (int i = 0; i &lt; lengthOfInputArray; i++)
            for (int j = 0; j &lt; lengthOfInputArray - 1; j++) {
                if (arr[j] &gt; arr[j + 1]) {
                    // swap(arr[j], arr[j + 1])
                    arr[j] = returnFirst(arr[j + 1], arr[j + 1] = arr[j]);
                }
            }
    }

}
In addition to the other answers, there are a couple common bubble sort optimizations that you can employ.

First, your inner loop steps through the entire array each time. This is unnecessary because each pass through the array is guaranteed to sink the largest value to the bottom, so the tail of the array is always sorted. You can avoid processing the sorted bits of the array by using 

for (int j = 0; j &lt; lengthOfInputArray - (i + 1); j++) {
    ...
}


for the inner loop.

Second, you can shortcut processing by ending when the inner loop encounters no swaps.

public static void bubbleSort(int[] arr) {
    boolean swapped = false;

    for (int i = 0; i &lt; arr.length; i++) {
        swapped = false;
        for (int j = 0; j &lt; arr.length - (i + 1); j++) {
            if (arr[j] &gt; arr[j + 1]) {
                if (swap(arr, j, j + 1)) {
                    swapped = true;
                }
            }
        }
        if (!swapped) {
            break;
        }
    }
}


This only happens when the array is fully sorted. So if you start out with a sorted array, then you only have to run through the array once.

Third, the outer loop does not have to be processed once for each element in the array. Take a five element array for example. It is iterated in the following manner:

value of i      max j required
----------      --------------
    0                 3
    1                 2
    2                 1
    3                 0


So the outer loop only needs to be processed arr.length - 1 times. The final optimized bubble sort would look something like this:

public static void bubbleSort(int[] arr) {
    boolean swapped = false;

    for (int i = 0; i &lt; arr.length - 1; i++) {
        swapped = false;
        for (int j = 0; j &lt; arr.length - (i + 1); j++) {
            if (arr[j] &gt; arr[j + 1]) {
                if (swap(arr, j, j + 1)) {
                    swapped = true;
                }
            }
        }
        if (!swapped) {
            break;
        }
    }
}