## Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

### Insertion Sort in Java

Before Sorting, Input :0 10 2 9 3 8 1 6 5 7 4   the first element is sorted here
0 10 2 9 3 8 1 6 5 7 4    first 2 elements are sorted
0 2 10 9 3 8 1 6 5 7 4   and so on….
0 2 9 10 3 8 1 6 5 7 4
0 2 3 9 10 8 1 6 5 7 4
0 2 3 8 9 10 1 6 5 7 4
0 1 2 3 8 9 10 6 5 7 4
0 1 2 3 6 8 9 10 5 7 4
0 1 2 3 5 6 8 9 10 7 4
0 1 2 3 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9 10
After Sorting, output :
0 1 2 3 4 5 6 7 8 9 10

` /**     * every element before the index is sorted. so select the number at the     * index location and put it in appropriate position     *      * @param numbers     */    private static void sortList(Integer[] numbers) {        for (int index = 0; index < numbers.length; index++) {            for (int sortIndex = index; sortIndex > 0; sortIndex--) {                if (numbers[sortIndex] < numbers[sortIndex - 1]) {                    swap(sortIndex, sortIndex - 1, numbers);                } else {                    break;                }            }            printArray(numbers);        }    }    /**     * swap 2 numbers at the index     *      * @param indexOne     * @param indexTwo     * @param numbers     */    private static void swap(int indexOne, int indexTwo, Integer[] numbers) {        Integer temp = numbers[indexOne];        numbers[indexOne] = numbers[indexTwo];        numbers[indexTwo] = temp;    }`