1 /****************************************************************************** 2 * Compilation: javac Selection.java 3 * Execution: java Selection < input.txt 4 * Dependencies: StdOut.java StdIn.java 5 * Data files: http://algs4.cs.princeton.edu/21elementary/tiny.txt 6 * http://algs4.cs.princeton.edu/21elementary/words3.txt 7 * 8 * Sorts a sequence of strings from standard input using selection sort. 9 * 10 * % more tiny.txt 11 * S O R T E X A M P L E 12 * 13 * % java Selection < tiny.txt 14 * A E E L M O P R S T X [ one string per line ] 15 * 16 * % more words3.txt 17 * bed bug dad yes zoo ... all bad yet 18 * 19 * % java Selection < words3.txt 20 * all bad bed bug dad ... yes yet zoo [ one string per line ] 21 * 22 ******************************************************************************/ 23 24 package edu.princeton.cs.algs4; 25 26 import java.util.Comparator; 27 28 /** 29 * The {@code Selection} class provides static methods for sorting an 30 * array using selection sort. 31 * <p> 32 * For additional documentation, see <a href="http://algs4.cs.princeton.edu/21elementary">Section 2.1</a> of 33 * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne. 34 * 35 * @author Robert Sedgewick 36 * @author Kevin Wayne 37 */ 38 public class Selection { 39 40 // This class should not be instantiated. 41 private Selection() { } 42 43 /** 44 * Rearranges the array in ascending order, using the natural order. 45 * @param a the array to be sorted 46 */ 47 public static void sort(Comparable[] a) {//因为字符串实现了comparable接口故可以使用comparable作为父类 是多态的体现 48 int n = a.length; 49 for (int i = 0; i < n; i++) { 50 int min = i; 51 for (int j = i+1; j < n; j++) { 52 if (less(a[j], a[min])) min = j; 53 } 54 exch(a, i, min); 55 assert isSorted(a, 0, i); 56 } 57 assert isSorted(a); 58 } 59 60 /** 61 * Rearranges the array in ascending order, using a comparator. 62 * @param a the array 63 * @param comparator the comparator specifying the order 64 */ 65 public static void sort(Object[] a, Comparator comparator) { 66 int n = a.length; 67 for (int i = 0; i < n; i++) { 68 int min = i; 69 for (int j = i+1; j < n; j++) { 70 if (less(comparator, a[j], a[min])) min = j; 71 } 72 exch(a, i, min); 73 assert isSorted(a, comparator, 0, i); 74 } 75 assert isSorted(a, comparator); 76 } 77 78 79 /*************************************************************************** 80 * Helper sorting functions. 81 ***************************************************************************/ 82 83 // is v < w ? 84 private static boolean less(Comparable v, Comparable w) { 85 return v.compareTo(w) < 0;//若v小于w则返回true 86 } 87 88 // is v < w ? 89 private static boolean less(Comparator comparator, Object v, Object w) { 90 return comparator.compare(v, w) < 0; 91 } 92 93 94 // exchange a[i] and a[j] 95 private static void exch(Object[] a, int i, int j) { 96 Object swap = a[i]; 97 a[i] = a[j]; 98 a[j] = swap; 99 } 100 101 102 /*************************************************************************** 103 * Check if array is sorted - useful for debugging. 104 ***************************************************************************/ 105 106 // is the array a[] sorted? 107 private static boolean isSorted(Comparable[] a) { 108 return isSorted(a, 0, a.length - 1); 109 } 110 111 // is the array sorted from a[lo] to a[hi] 112 private static boolean isSorted(Comparable[] a, int lo, int hi) { 113 for (int i = lo + 1; i <= hi; i++) 114 if (less(a[i], a[i-1])) return false; 115 return true; 116 } 117 118 // is the array a[] sorted? 119 private static boolean isSorted(Object[] a, Comparator comparator) { 120 return isSorted(a, comparator, 0, a.length - 1); 121 } 122 123 // is the array sorted from a[lo] to a[hi] 124 private static boolean isSorted(Object[] a, Comparator comparator, int lo, int hi) { 125 for (int i = lo + 1; i <= hi; i++) 126 if (less(comparator, a[i], a[i-1])) return false; 127 return true; 128 } 129 130 131 132 // print array to standard output 133 private static void show(Comparable[] a) { 134 for (int i = 0; i < a.length; i++) { 135 StdOut.print(a[i]); 136 } 137 } 138 139 /** 140 * Reads in a sequence of strings from standard input; selection sorts them; 141 * and prints them to standard output in ascending order. 142 * 143 * @param args the command-line arguments 144 */ 145 public static void main(String[] args) { 146 String[] a = StdIn.readAllStrings();//使用输入流的方式进行输入,当结束时应该按住ctrl+c结束输入流 147 show(a); 148 System.out.println(); 149 Selection.sort(a); 150 show(a); 151 } 152 } 153 154 /****************************************************************************** 155 * Copyright 2002-2016, Robert Sedgewick and Kevin Wayne. 156 * 157 * This file is part of algs4.jar, which accompanies the textbook 158 * 159 * Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne, 160 * Addison-Wesley Professional, 2011, ISBN 0-321-57351-X. 161 * http://algs4.cs.princeton.edu 162 * 163 * 164 * algs4.jar is free software: you can redistribute it and/or modify 165 * it under the terms of the GNU General Public License as published by 166 * the Free Software Foundation, either version 3 of the License, or 167 * (at your option) any later version. 168 * 169 * algs4.jar is distributed in the hope that it will be useful, 170 * but WITHOUT ANY WARRANTY; without even the implied warranty of 171 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 172 * GNU General Public License for more details. 173 * 174 * You should have received a copy of the GNU General Public License 175 * along with algs4.jar. If not, see http://www.gnu.org/licenses. 176 ******************************************************************************/