public class NumSort {
private static void SelectionNum(int[] a)
{
int k,temp; //k来记录最小位置,temp用于交换
for(int i = 0;i < a.length; i++) {
k = i;
for(int j = k + 1; j < a.length; j++) {
if(a[j] < a[k])
k = j;
}
//若最小位不是i,则与k所指向的最小位交换
if(k != i) {
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
}
private static void print(int[] a) {
for(int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
}
public static void main(String[] args) {
int a[] = {4,5,3,1,6,2,8};
NumSort.print(a);
NumSort.SelectionNum(a); //排序后
NumSort.print(a);
}
}
输出结果:
4 5 3 1 6 2 8
1 2 3 4 5 6 8