import java.util.Arrays;
public class SelectSort {
public static void selectSort(int arr[]) {
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
int min = arr[i];
for (int j = i + 1; j < arr.length; j++) {
if (min > arr[j]) {
min = arr[j]; //重置min
minIndex = j; //重置minIndex
}
}
if (minIndex != i) {
arr[minIndex] = arr[i];
arr[i] = min;
}
System.out.println("第" + (i + 1) + "轮后");
System.out.println(Arrays.toString(arr));
}
}
public static void main(String[] args) {
int[] arr = {101, 34, 119, 1};
System.out.println("排序前");
System.out.println(Arrays.toString(arr));
selectSort(arr);
System.out.println("排序后");
System.out.println(Arrays.toString(arr));
}
}
01-19
1490
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-16
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交