/*
* 用后一个数和前面的数进行比较,找到数组中后一个数比值小的数的位置,则进行数组整体后移。
* 最坏复杂度为:n*n
* */
public class InsertSort {
public static void main(String[] args) {
int[] test=new int[] {5,2,4,6,1,3};
int key=0;
for(int j=1;j<test.length;j++) {
key=test[j];
int i=j-1;
while(i>=0 && test[i]>key) {
test[i+1]=test[i];
i=i-1;
}
test[i+1]=key;
}
System.out.println(test);
}
}
java实现算法笔记之一--插入排序
最新推荐文章于 2024-05-21 11:43:48 发布