概念
每一趟只能确定一个数归位。即第一趟只能确定经末位上的数归位,第二趟只能将倒数第二位上的数归位,依次类推。如果有n个数,需要n-1次数归位,即需要n-1趟。
图解
稳定性
在两个数交换时,如果两个数相同,那么不会进行位置交换,所以认为冒泡排序是稳定排序
CODE
public class SelectionSort {
private static final List<Integer> LIST = new ArrayList<>() {{
add(2);
add(3);
add(4);
add(5);
add(8);
add(7);
add(4);
}};
public static void main(String[] args) {
// 外层for循环 i<LIST.size() 也可以写成 i<LIST.size()-1
for (int i = 0; i < LIST.size(); i++) {
for (int j = i + 1; j < LIST.size(); j++) {
if (LIST.get(i) > LIST.get(j)) {
swap(LIST, i, j);
}
}
}
System.out.println(LIST);
}
public static void swap(List<Integer> list, int fromIndex, int toIndex) {
int tempVal = list.get(fromIndex);
list.set(fromIndex, list.get(toIndex));
list.set(toIndex, tempVal);
}
}
extra
😶🌫️😶🌫️😶🌫️