import java.util.Arrays;
public class SelectionSort {
public static void main(String[] args) {
int[] array = new int[6];
for (int i = 0; i < 6; i++) {
array[i] = (int)(Math.random()*20);
}
System.out.println("待排序的数据为:"+Arrays.toString(array));
System.out.println("进行排序************");
selectionSort(array);
}
public static void selectionSort(int[] array) {
int len = array.length;
int minIndex = 0;
for (int i = 0; i < len - 1; i++) {
minIndex = i;
for (int j = i+1; j < len; j++) {
if(array[j] < array[minIndex]) {
minIndex = j;
}
}
if(i != minIndex) {
array[i] = array[i] + array[minIndex] - (array[minIndex] = array[i]);
}
System.out.println("第"+(i+1)+"轮排序后:"+ Arrays.toString(array));
}
}
}