/** * Child class to the Sort class in which implements a Insertion sort algorithm * * @author Joe Rivard * @version 2/27/03 */ import com.vsort.sorting.*; public class InsertionSort extends Sort { /** * a method that uses the Insertion sort algorithm */ public void sorter(SortPanel panel2) { int length = panel2.getLength(); for(int i = 1; i < length; i++){ int itemtoInsert = panel2.get(i); panel2.swapWithTmp(i); swaps++; int j = i - 1; while(j >= 0){ if(itemtoInsert < panel2.get(j)){ panel2.swap(j+1, j); swaps++; comp++; j--; } else break; } panel2.swapWithTmp(j+1); swaps++; } } /** * returns the number of swaps the program makes * @return int swaps */ public int getSwaps() { return swaps; } /** * a method gets the number of comparisons that occur * @return int num */ public int getComparisons() { return comp; } }