public class Test666 {
public static void main(String[] args) {
int[] list = {22,66,44,33,52,23,12,16,88,99,45,65,85,53,12,0};
selectionSort(list);
}
public static void selectionSort(int[] list) {
for (int i = 0; i < list.length - 1; i++) {
// 循环数组中的所有的数字,除了最后一个,所以此处是list.length-1
int index = i;// 存储索引
int temp = 0;// 临时变量
for (int j = i + 1; j < list.length; j++) {
// 循环索引为i的以后的所有的数字,与索引为i
// 的数字进行比较,如果比list[index]>list[j],用index保存它的索引
if (list[index] > list[j]) {
index = j;
}
}
// 循环一轮后,找到了那个数字比索引为i的值小,交换位置。即,将索引为index的值与索引为i的值互换位置。
temp = list[index];
list[index] = list[i];
list[i] = temp;
}
for (int i : list) {
System.out.println(i);
}
}
}
选择排序
最新推荐文章于 2024-09-28 22:14:04 发布