排序原理
-
每一次遍历的过程中,假定第一个元素为最小值,将第一个元素和之后每一个元素作比较,一直到最后,可以找出最小值
-
交换位置
public class Selection { public static void sort(Integer[] a) { for (int i =0; i < a.length; i++) { int minindex=i; for (int j = i; j < a.length; j++) { if(greater(a[minindex],a[j])) { minindex=j; } } exch(a,minindex,i); } } //判断两数大小 private static boolean greater(Comparable v,Comparable w) { return v.compareTo(w)>0; } //交换数组a,i处和j处的值 private static void exch(Integer[] b,int i,int j) { int temp; temp=b[i]; b[i]=b[j]; b[j]=temp; } }
public class test01 { public static void main(String[] args) { Integer[] selection={45,11,0,7,7,-6,78,1,46,-6}; Selection.sort(selection); System.out.println(Arrays.toString(selection)); } }