/**
* 选择排序算法:
* 在数组中选出最小(大)的一个元素并与第一个元素交换,然后找出第二小的元素与第二个元素交换……
* Created by hasee on 2017/6/27.
*/
public class SelectSort {
public static void main(String[] args) {
doSort(2,1,5,3,6,8,7,9,4);
}
public static void doSort(int... a){
for(int i=0; i<a.length; i++){
int minIndex = i;
for(int j=i+1; j<a.length; j++){
if(a[j] < a[minIndex]){
minIndex = j;
}
}
if(i != minIndex){
int temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
}
//循环打印完成排序的数组
for(int i=0; i<a.length; i++){
System.out.println(a[i]);
}
}
}
选择排序
最新推荐文章于 2025-05-01 20:06:47 发布