import java.util.*; public class TSortMain { public static void main (String[ ] args) { String[ ] words = {"yes", "true", "maybe", "relax", "heavenly", "good", "halcyon"}; insertionSort (words); //selectionSort (words); //bubbleSort (words); //selectionSort (words, new ByLength()); for (String s : words) System.out.print (s + " "); } // method main /** * Sorts a specified array of T values into the natural order. * The worstTime(n) is O(n * n). * * @param x - the array to be sorted. * */ public static void insertionSort (T[ ] x) { for (int i = 1; i < x.length; i++) for (int j = i; j > 0 && ((Comparable)x [j -1]).compareTo (x [j]) > 0; j--) swap (x, j, j -1); } // method insertionSort /** * Swaps two specified elements in a specified array. * * @param x - the array in which the two elements are to be swapped. * @param a - the index of one of the elements to be swapped. * @param b - the index of the other element to be swapped. * */ public static void swap (T [ ] x, int a, int b) { T t = x[a]; x[a] = x[b]; x[b] = t; } // method swap /** * Sorts a specified array of T values into the natural order. * The worstTime(n) is O(n * n). * * @param x - the array to be sorted. * */ public static void selectionSort (T[ ] x) { // Make x [0...i] sorted and <= x [i + 1... x.length-1]: for (int i = 0; i < x.length - 1; i++) { int pos = i; for (int j = i + 1; j < x.length; j++) if (((Comparable)x [j]).compareTo ( x [pos]) < 0) pos = j; swap (x, i, pos); } // for i } // method selectionSort /** * Sorts a specified array of T values into the natural order. * The worstTime(n) is O(n * n). * * @param x - the array to be sorted. * */ public static void bubbleSort (T[ ] x) { int finalSwapPos = x.length - 1, swapPos; while (finalSwapPos > 0) { swapPos = 0; for (int i = 0; i < finalSwapPos; i++) if (((Comparable)x [i]).compareTo ( x [i + 1]) > 0) { swap (x, i, i + 1); swapPos = i; } // if finalSwapPos = swapPos; } // while } // method bubbleSort /** * Sorts a specified array into the order specified by a specified Comparator * object. * The worstTime(n) is O(n * n). * * @param x - the array to be sorted. * @param comp - the Comparator object used for ordering. * */ public static void selectionSort (T [ ] x, Comparator comp) { // Make x [0...i] sorted and <= x [i+1 ... x.length-1]: for (int i = 0; i < x.length - 1; i++) { int pos = i; for (int j = i + 1; j < x.length; j++) if (comp.compare (x [j], x [pos]) < 0) pos = j; swap (x, i, pos); } // for i } // method selectionSort } // class TSortMain class ByLength implements Comparator { /** * Compares two specified String objects lexicographically if they have the * same length, and otherwise returns the difference in their lengths. * * @param s1 - one of the specified String objects. * @param s2 - the other specified String object. * * @return s1.compareTo (s2) if s1 and s2 have the same length; * otherwise, return s1.length() - s2.length(). * */ public int compare (String s1, String s2) { int len1 = s1.length(), len2 = s2.length(); if (len1 == len2) return s1.compareTo (s2); return len1 - len2; } // method compare } // class ByLength