//******************************************************************** // Sorts.java Author: Lewis/Loftus edited by Brenneman // // 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. Added the size parameter, which is the // number of elements in the array to be sorted. //----------------------------------------------------------------- public static void insertionSort (Comparable[] objects, int size) { for (int index = 1; index < size; 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; } } }