冒泡排序(Bubble Sort)
动图演示地址(https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html)
代码结构
源码
前置条件:父接口和测试类从上篇文章获取(https://blog.csdn.net/jt781861965/article/details/117051443)
package suanfa.paixu;
public class MaoPaoTest implements Sort {
public static void main(String[] args) {
Test.test(new MaoPaoTest());
}
public void sort(int[] arr) {
if (arr == null || arr.length < 2) {
return;
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int num = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = num;
}
}
}
}
}