1 package algorithms.elementary21; 2 3 import algorithms.util.StdIn; 4 import algorithms.util.StdOut; 5 6 /****************************************************************************** 7 * Compilation: javac InsertionX.java 8 * Execution: java InsertionX < input.txt 9 * Dependencies: StdOut.java StdIn.java 10 * Data files: http://algs4.cs.princeton.edu/21sort/tiny.txt 11 * http://algs4.cs.princeton.edu/21sort/words3.txt 12 * 13 * Sorts a sequence of strings from standard input using an optimized 14 * version of insertion sort that uses half exchanges instead of 15 * full exchanges to reduce data movement.. 16 * 17 * % more tiny.txt 18 * S O R T E X A M P L E 19 * 20 * % java InsertionX < tiny.txt 21 * A E E L M O P R S T X [ one string per line ] 22 * 23 * % more words3.txt 24 * bed bug dad yes zoo ... all bad yet 25 * 26 * % java InsertionX < words3.txt 27 * all bad bed bug dad ... yes yet zoo [ one string per line ] 28 * 29 ******************************************************************************/ 30 /** 31 * The <tt>InsertionX</tt> class provides static methods for sorting 32 * an array using an optimized version of insertion sort (with half exchanges 33 * and a sentinel). 34 * <p> 35 * For additional documentation, see <a href="http://algs4.cs.princeton.edu/21elementary">Section 2.1</a> of 36 * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne. 37 * 38 * @author Robert Sedgewick 39 * @author Kevin Wayne 40 */ 41 42 public class InsertionX { 43 44 // This class should not be instantiated. 45 private InsertionX() { } 46 47 /** 48 * Rearranges the array in ascending order, using the natural order. 49 * @param a the array to be sorted 50 */ 51 public static void sort(Comparable[] a) { 52 int N = a.length; 53 54 // put smallest element in position to serve as sentinel 55 int exchanges = 0; 56 for (int i = N-1; i > 0; i--) { 57 if (less(a[i], a[i-1])) { 58 exch(a, i, i-1); 59 exchanges++; 60 } 61 } 62 if (exchanges == 0) return; 63 64 65 // insertion sort with half-exchanges 66 for (int i = 2; i < N; i++) { 67 Comparable v = a[i]; 68 int j = i; 69 while (less(v, a[j-1])) { 70 a[j] = a[j-1]; 71 j--; 72 } 73 a[j] = v; 74 } 75 76 assert isSorted(a); 77 } 78 79 80 /*************************************************************************** 81 * Helper sorting functions. 82 ***************************************************************************/ 83 84 // is v < w ? 85 private static boolean less(Comparable v, Comparable w) { 86 return v.compareTo(w) < 0; 87 } 88 89 // exchange a[i] and a[j] 90 private static void exch(Object[] a, int i, int j) { 91 Object swap = a[i]; 92 a[i] = a[j]; 93 a[j] = swap; 94 } 95 96 97 /*************************************************************************** 98 * Check if array is sorted - useful for debugging. 99 ***************************************************************************/ 100 private static boolean isSorted(Comparable[] a) { 101 for (int i = 1; i < a.length; i++) 102 if (less(a[i], a[i-1])) return false; 103 return true; 104 } 105 106 // print array to standard output 107 private static void show(Comparable[] a) { 108 for (int i = 0; i < a.length; i++) { 109 StdOut.println(a[i]); 110 } 111 } 112 113 /** 114 * Reads in a sequence of strings from standard input; insertion sorts them; 115 * and prints them to standard output in ascending order. 116 */ 117 public static void main(String[] args) { 118 String[] a = StdIn.readAllStrings(); 119 InsertionX.sort(a); 120 show(a); 121 } 122 123 }
用二分查找法改进
1 package algorithms.elementary21; 2 3 /****************************************************************************** 4 * Compilation: javac BinaryInsertion.java 5 * Execution: java BinaryInsertion < input.txt 6 * Dependencies: StdOut.java StdIn.java 7 * Data files: http://algs4.cs.princeton.edu/21sort/tiny.txt 8 * http://algs4.cs.princeton.edu/21sort/words3.txt 9 * 10 * Sorts a sequence of strings from standard input using 11 * binary insertion sort with half exchanges. 12 * 13 * % more tiny.txt 14 * S O R T E X A M P L E 15 * 16 * % java BinaryInsertion < tiny.txt 17 * A E E L M O P R S T X [ one string per line ] 18 * 19 * % more words3.txt 20 * bed bug dad yes zoo ... all bad yet 21 * 22 * % java BinaryInsertion < words3.txt 23 * all bad bed bug dad ... yes yet zoo [ one string per line ] 24 * 25 ******************************************************************************/ 26 27 import algorithms.util.StdIn; 28 import algorithms.util.StdOut; 29 30 /** 31 * The <tt>BinaryInsertion</tt> class provides a static method for sorting an 32 * array using an optimized binary insertion sort with half exchanges. 33 * <p> 34 * This implementation makes ~ N lg N compares for any array of length N. 35 * However, in the worst case, the running time is quadratic because the 36 * number of array accesses can be proportional to N^2 (e.g, if the array 37 * is reverse sorted). As such, it is not suitable for sorting large 38 * arrays (unless the number of inversions is small). 39 * <p> 40 * The sorting algorithm is stable and uses O(1) extra memory. 41 * <p> 42 * For additional documentation, see <a href="http://algs4.cs.princeton.edu/21elementary">Section 2.1</a> of 43 * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne. 44 * 45 * @author Ivan Pesin 46 * @author Robert Sedgewick 47 * @author Kevin Wayne 48 */ 49 public class BinaryInsertion { 50 51 // This class should not be instantiated. 52 private BinaryInsertion() { } 53 54 /** 55 * Rearranges the array in ascending order, using the natural order. 56 * @param a the array to be sorted 57 */ 58 public static void sort(Comparable[] a) { 59 int N = a.length; 60 for (int i = 1; i < N; i++) { 61 62 // binary search to determine index j at which to insert a[i] 63 Comparable v = a[i]; 64 int lo = 0, hi = i; 65 while (lo < hi) { 66 int mid = lo + (hi - lo) / 2; 67 if (less(v, a[mid])) hi = mid; 68 else lo = mid + 1; 69 } 70 71 // insetion sort with "half exchanges" 72 // (insert a[i] at index j and shift a[j], ..., a[i-1] to right) 73 for (int j = i; j > lo; --j) 74 a[j] = a[j-1]; 75 a[lo] = v; 76 } 77 assert isSorted(a); 78 } 79 80 81 82 /*************************************************************************** 83 * Helper sorting functions. 84 ***************************************************************************/ 85 86 // is v < w ? 87 private static boolean less(Comparable v, Comparable w) { 88 return v.compareTo(w) < 0; 89 } 90 91 // exchange a[i] and a[j] 92 private static void exch(Object[] a, int i, int j) { 93 Object swap = a[i]; 94 a[i] = a[j]; 95 a[j] = swap; 96 } 97 98 // exchange a[i] and a[j] (for indirect sort) 99 private static void exch(int[] a, int i, int j) { 100 int swap = a[i]; 101 a[i] = a[j]; 102 a[j] = swap; 103 } 104 105 /*************************************************************************** 106 * Check if array is sorted - useful for debugging. 107 ***************************************************************************/ 108 private static boolean isSorted(Comparable[] a) { 109 return isSorted(a, 0, a.length - 1); 110 } 111 112 // is the array sorted from a[lo] to a[hi] 113 private static boolean isSorted(Comparable[] a, int lo, int hi) { 114 for (int i = lo+1; i <= hi; i++) 115 if (less(a[i], a[i-1])) return false; 116 return true; 117 } 118 119 // print array to standard output 120 private static void show(Comparable[] a) { 121 for (int i = 0; i < a.length; i++) { 122 StdOut.println(a[i]); 123 } 124 } 125 126 /** 127 * Reads in a sequence of strings from standard input; insertion sorts them; 128 * and prints them to standard output in ascending order. 129 */ 130 public static void main(String[] args) { 131 String[] a = StdIn.readAllStrings(); 132 BinaryInsertion.sort(a); 133 show(a); 134 } 135 }