package net.bunnie; public class Sort { public static int[] insertion (int[] input) { /* Insertion sort * Go element by element * Put in respective place * according to other sorted elements */ int temp; int j; for (int i = 1; i < input.length; i++) { temp = input[i]; for (j = i; (j > 0) && (temp < input[j - 1]); j--) { input[j] = input[j - 1]; } input[j] = temp; } return input; } public static int[] bubble (int[] input) { /* Bubble sort * Bubble each element up to top * using swap method * Outer loop: loop through input.length times * Inner loop: swaps */ int temp; int min = input[0]; for (int i = 0; i < input.length; i++) { for (int j = input.length - 1; j > i; j--) { if (input[j] < input[j - 1]) { temp = input[j - 1]; input[j - 1] = input[j]; input[j] = temp; } } } return input; } public static int[] selection (int[] input) { /* Selection sort * Minimum value swapping */ int min; int temp; for (int i = 0; i < input.length; i++) { min = i; for (int j = i + 1; j < input.length; j++) { if (input[j] < input[min]) { min = j; } } // save time, who needs to swap if it is the smallest? if (i != min) { temp = input[i]; input[i] = input[min]; input[min] = temp; } } return input; } }