/* * CS220_SimpleSortingAlgorithms_Interface.java * * Created on Aug 18, 2007 * * by Donald Yessick */ /** * CS220_SimpleSortingAlgorithms_Interface * @author Donald Yessick * @ */ public interface CS220_SimpleSortingAlgorithms_Interface { /* * THE NAIVE BUBBLE SORT ALGORITHM * repeat N-1 times * for i in 1..N-1 * if A[i] < A[i-1] * swap A[i] with A[i-1] */ public void bubbleSort(Comparable[] unsorted, int n); /* * THE INSERTION SORT ALGORITHM * numSorted=0; * do { * position = numSorted; * insertItem = A[position] * while position > 0 && A[position-1] < insertItem * move A[Position-1] to A[Position] * position-- * A[position] = insertItem * numSorted++; * } repeat until numSorted is N */ public void insertionSort(Comparable[] unsorted, int n); /* * THE SELECTION SORT ALGORITHM * * Version 1: Select Max * * numUnsorted = N * do { * last = numUnsorted-1; * max = 0; * for k in 1..last * if A[k] > A[max] * max = k * swap A[last] with A[max] * numUnsorted-- * } until numUnsorted is 0; * * Version 2: Select Min * * for next in 0..N-1 * min = next; * for k in next..N-1 * if A[k] < A[min] * min = k * swap A[next] with A[max] */ public void selectionSort(Comparable[] unsorted, int n); /* * THE RANK SORT ALGORITHM * * ** step one rank elements * int[N] rank = {0} * for int left in 0..N-2 * for right in left+1..N-1 * if A[left] > A[right] * rank[left]++ //count values smaller * else * rank[right]++//count smaller or equal and left * * * ** step two move ranked elements into place * for all k * sorted[k] = unsorted[rank[k]] */ public void rankSort(Comparable[] unsorted, int n); }