//lilywangcn
public class InsertSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array=new int[]{10,30,20,4,9,-1,6,15};
for(int i=1;i<array.length;i++){
int tmp=array[i];
int j=i-1;
while(j>=0 && array[j]>tmp){
array[j+1]=array[j];
--j;
}
array[j+1]=tmp;
}
for(int i=0;i<array.length;i++){
System.out.print(array[i]+" ");
}
}
}
算法复杂度:O(n*n),算法稳定。
运行结果:
-1 4 6 9 10 15 20 30