选择排序每次将首个数与后面的数字依次比较,每次找到最大(或最小)值。
图解如下:
具体代码:
public class SelectSort {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
int[] a = { 2, 11, 5, 4, 7, 8, 9, 6 };
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i : a) {
System.out.print(i + " ");
}
long endTime = System.currentTimeMillis();
System.out.println();
System.out.println("程序运行时间:"+(endTime-startTime)+"ms");
}
}