算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)
一、介绍
1.算法的时间和空间间复杂度
2.特点
Running time is insensitive to input. The process of finding the smallest item on one
pass through the array does not give much information about where the smallest item
might be on the next pass. This property can be disadvantageous in some situations.
For example, the person using the sort client might be surprised to realize that it takes
about as long to run selection sort for an array that is already in order or for an array
with all keys equal as it does for a randomly-ordered array! As we shall see, other algo-
rithms are better able to take advantage of initial order in the input.
Data movement is minimal. Each of the N exchanges changes the value of two array
entries, so selection sort uses N exchanges—the number of array accesses is a linear
function of the array size. None of the other sorting algorithms that we consider have
this property (most involve linearithmic or quadratic growth).
3.过程
二、代码
1 package algorithms.elementary21; 2 3 /****************************************************************************** 4 * Compilation: javac Selection.java 5 * Execution: java Selection < 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 selection sort. 11 * 12 * % more tiny.txt 13 * S O R T E X A M P L E 14 * 15 * % java Selection < tiny.txt 16 * A E E L M O P R S T X [ one string per line ] 17 * 18 * % more words3.txt 19 * bed bug dad yes zoo ... all bad yet 20 * 21 * % java Selection < words3.txt 22 * all bad bed bug dad ... yes yet zoo [ one string per line ] 23 * 24 ******************************************************************************/ 25 26 import java.util.Comparator; 27 28 import algorithms.util.StdIn; 29 import algorithms.util.StdOut; 30 31 /** 32 * The <tt>Selection</tt> class provides static methods for sorting an 33 * array using selection sort. 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 public class Selection { 42 43 // This class should not be instantiated. 44 private Selection() { } 45 46 /** 47 * Rearranges the array in ascending order, using the natural order. 48 * @param a the array to be sorted 49 */ 50 public static void sort(Comparable[] a) { 51 int N = a.length; 52 for (int i = 0; i < N; i++) { 53 int min = i; 54 for (int j = i+1; j < N; j++) { 55 if (less(a[j], a[min])) min = j; 56 } 57 exch(a, i, min); 58 assert isSorted(a, 0, i); 59 } 60 assert isSorted(a); 61 } 62 63 /** 64 * Rearranges the array in ascending order, using a comparator. 65 * @param a the array 66 * @param c the comparator specifying the order 67 */ 68 public static void sort(Object[] a, Comparator c) { 69 int N = a.length; 70 for (int i = 0; i < N; i++) { 71 int min = i; 72 for (int j = i+1; j < N; j++) { 73 if (less(c, a[j], a[min])) min = j; 74 } 75 exch(a, i, min); 76 assert isSorted(a, c, 0, i); 77 } 78 assert isSorted(a, c); 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 // is v < w ? 92 private static boolean less(Comparator c, Object v, Object w) { 93 return c.compare(v, w) < 0; 94 } 95 96 97 // exchange a[i] and a[j] 98 private static void exch(Object[] a, int i, int j) { 99 Object swap = a[i]; 100 a[i] = a[j]; 101 a[j] = swap; 102 } 103 104 105 /*************************************************************************** 106 * Check if array is sorted - useful for debugging. 107 ***************************************************************************/ 108 109 // is the array a[] sorted? 110 private static boolean isSorted(Comparable[] a) { 111 return isSorted(a, 0, a.length - 1); 112 } 113 114 // is the array sorted from a[lo] to a[hi] 115 private static boolean isSorted(Comparable[] a, int lo, int hi) { 116 for (int i = lo + 1; i <= hi; i++) 117 if (less(a[i], a[i-1])) return false; 118 return true; 119 } 120 121 // is the array a[] sorted? 122 private static boolean isSorted(Object[] a, Comparator c) { 123 return isSorted(a, c, 0, a.length - 1); 124 } 125 126 // is the array sorted from a[lo] to a[hi] 127 private static boolean isSorted(Object[] a, Comparator c, int lo, int hi) { 128 for (int i = lo + 1; i <= hi; i++) 129 if (less(c, a[i], a[i-1])) return false; 130 return true; 131 } 132 133 134 135 // print array to standard output 136 private static void show(Comparable[] a) { 137 for (int i = 0; i < a.length; i++) { 138 StdOut.println(a[i]); 139 } 140 } 141 142 /** 143 * Reads in a sequence of strings from standard input; selection sorts them; 144 * and prints them to standard output in ascending order. 145 */ 146 public static void main(String[] args) { 147 String[] a = StdIn.readAllStrings(); 148 Selection.sort(a); 149 show(a); 150 } 151 }