public class SelectSort {
public static void main(String[] args) {
int[] a = {9,5,1,4,3,8,2,7,6};
sort(a);
print(a);
}
static void sort(int[] a){
for (int i=0; i<a.length; i++){ //第一个元素开始
int min = a[i];
int index = i;
for (int j=i+1; j<a.length; j++) { //从第二个元素开始往后比较
if (min>a[j]){
min = a[j]; //min保存最小值
index = j; //index 保存元素序号
}
}
a[index] = a[i]; // 交换
a[i] = min;
}
}
static void print(int[] arr){
for (int i = 0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}