选择排序:从第一个元素逐次选定为最小,然后将其依次与后面元素比较
package com.mao.bubble;
public class SelectionSort {
public static void main(String[] args) {
int[] array = {23,1,243,12,9,5,7};
//外层控制:比较轮数
for (int i = 0; i < array.length - 1; i++) {
//内层控制:每一轮的比较过程
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[i]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
printArray(array);
}
}
//打印数组
public static void printArray(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + "\t");
}
System.out.println();
}
}