您的位置:首页 > 其它

优化的选择排序——SelectionSortSecond

2016-12-26 12:31 295 查看
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");
}

}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: