先找出最小的值并将其与a[1]中的元素进行交换,接着找出a数组中次最小的元素与a[2]进行交换,一次找出其他的
/**
* 选择排序
* 对指定数组进行排序
* @param a 要排序的数组
*/
public static int[] selectSort(int[] a ){
if(a==null||a.length<0){
return null;
}
for(int i=0 ; i<a.length-1; i++){ //
int key = i; //默认将第一个元素的作为最小值
for(int j = i+1; j<a.length; j++){ //
if(a[key]>a[j]){ //
key = j; //选择出最小的值的下标
}
}
if(i!=key){
swap(a,key,i); //交换值
}
}
return a;
}
/**
* 交换数组中的两个数
* @param a 要交换的数组
* @param x 要交换的索引
* @param y 要交换的索引
*/
public static void swap(int[] a ,int x ,int y){
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}