// Play with insertion sort // Beth Katz - April 2009 public class Sort1 { public static void main(String[] args) { // initialize arrays int[] bumpy = {79, 45, 67, 92, 93, 35, 78, 93, 89, 67, 78}; int[] smooth = {82, 86, 84, 85, 83, 84, 85, 83}; print(bumpy, "Bumpy before"); insertionSort(bumpy); print(bumpy, "Bumpy after"); } // insertion sort public static void insertionSort(int[ ] list) { for (int i = 1; i < list.length; i++) { // placing list[i] in correct place int current = list[i]; int j = 0; while (j < i && list[j] <= current) { j++; } // move over later elements to make room for (int k = i-1; k >= j; k--) { list[k+1] = list[k]; } list[j] = current; } } // prints a list with items separated by spaces public static void print(int[] list, String label) { System.out.print(label + " has " + list.length + " items: "); for (int item : list) { System.out.print(item + " "); } System.out.println( ); } } /****** output ****** Bumpy before has 11 items: 79 45 67 92 93 35 78 93 89 67 78 Bumpy after has 11 items: 35 45 67 67 78 78 79 89 92 93 93 *********************/