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