package sort.com;
public class SelectionSortSecond {
/**
*优化的选择排序,减少了交换次数
*
*/
public static void main(String[] args) {
int []a = {5,-7,0,12,63,23,12,-6,1,-25};
for (int i= 0; i< a.length-1; i++){
//定义一个动态的索引,
int minIndex = i;
for (int j = i + 1; j < a.length; j++){
if(a[minIndex] > a[j]){
minIndex = j;
}
}
//利用if语句将最小的放在每次比较结束的了序列的最前面,也就时已经有序的序列的最后面
if( minIndex != i){
int temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
}
//遍历并输出数组
for ( int v :a){
System.out.print(v + "\t");
}
}
}