//******************************************************************** // Sorts.java Author: Lewis/Loftus // // Demonstrates the selection sort and insertion sort algorithms, // as well as a generic object sort. //******************************************************************** public class Sorts { //----------------------------------------------------------------- // Sorts the specified array of objects using the insertion // sort algorithm. //----------------------------------------------------------------- public static void insertionSort (Comparable[] objects) { for (int index = 1; index < objects.length; index++) { Comparable key = objects[index]; int position = index; // shift larger values to the right while (position > 0 && objects[position-1].compareTo(key) > 0) { objects[position] = objects[position-1]; position--; } objects[position] = key; } } }