一.基本思想
类似于抓扑克牌,对于未排序的数据,在已排序的数据中从前向后扫描,并插入相应位置
二.概览
分类:内部比较排序
数据结构:数组
最坏时间复杂度:最坏情况是输入序列为降序的,此时时间复杂度O(n^2)
最优时间复杂度:最好情况输入序列是升序排列,此时时间复杂度O(n)
平均时间复杂度:O(n^2)
所需辅助空间:O(1)
稳定性:稳定
三.代码
public class Insertion { public static void sort(int a[]){ int N = a.length; for(int i=1;i<N;i++){ for(int j=i;j>0 && a[j]<a[j-1];j--){ int t = a[j]; a[j] = a[j-1]; a[j-1] = t; } } } }