/**
* 选择排序
* 每次排序都把最大的数与 待排列的最后一个数交换位置
* 32 43 23 13 5
* 32 5 23 13 ,43
* 13 5 23 ,32 43
* 13 5 ,23 32 43 (23就是余下中最大的,不用换位置)
* 5 ,13 23 32 43
* 最后得到排序结果 5<13<23<32<43
* @author Administrator
*
*/
public class SelectSort {
public static void main(String[] args) {
int[] t = { 12, 20, 5, 16, 15, 1, 30, 45, 23, 9 };
SelectSort.sort(t);
for (int i : t) {
System.out.print(i+" ");
}
}
public static void sort(int[] array) {
for(int i=1;i<array.length;i++) {
int index=0;//定义默认索引
for(int j=1; j<=array.length-i;j++) {
//判断最大值,得到最大值的索引
if(array[j]>array[index]) {
index=j;
}
}
//最大的数与待排列的最后一个数交换位置
//交换在位置aray.length-i 与 index(最大值)上的两个数
int temp =array[array.length-i];
array[array.length-i]=array[index];
array[index]=temp;
}
}
}