排序原理 每一次遍历的过程中,都假定第一个索引处的元素是小值,和其他索引处的值依次进行比较,如果当前索引处 的值大于其他某个索引处的值,则假定其他某个索引出的值为小值,后可以找到小值所在的索引交换第一个索引处和小值所在的索引处的值 代码示例 public class Selection { public static void sort(Comparable[] a) { for (int i = 0; i < a.length - 1; i++) { int minIndex = i; for (int j = i + 1; j < a.length; j++) { if(greater(a[minIndex], a[j])) { minIndex = j; } } exch(a, i, minIndex); } } private static boolean greater(Comparable v, Comparable w) { return v.compareTo(w) > 0; } private static void exch(Comparable[] a, int i, int j) { Comparable t = a[i]; a[i] = a[j]; a[j] = t; } public static void main(String[] args) { Integer[] a = { 5, 6, 2, 0, 19, 33, 1, 8 }; Selection.sort(a); System.out.println(Arrays.toString(a)); }