import java.util.Arrays;
public class SelectSort {
public static void selectSort(int[] a) {
int i;
int j;
int temp = 0;
int flag = 0;
int n = a.length;
for (i = 0; i < n; i++) {
temp = a[i];
flag = i;
for (j = i+1; j<n; j++) {
if (a[j] < temp) {
temp = a[j];
flag = j;
}
}
if (flag != i) {
a[flag] = a[i];
a[i] = temp;
}
}
}
public static void main(String[] args) {
int i = 0;
int[] a = {5,4,9,8,6,0,1,3,2};
selectSort(a);
System.out.println(Arrays.toString(a));
}
}
【Java】选择排序
于 2022-07-19 15:10:21 首次发布