算法

1. 梗概:

  • 逐个把未排序中的记录插入到有序区间内
  • 其中要插入的过程要整体向后移动, 属于[use::轮换 算法]
  • 一种[father::插入类排序]

2. 图解

child::

3. 在c语言中的实现

梗概:

  1. 腾出位置
  2. 每一轮都判断是否应该放在这个位置 代码:
void insertion_sort(RecordType records[], int len){
	int i,j;
	RecordType temp;
	for (i=1;i<len;i++){//从第二个开始遍历
		temp = records[i];//为轮换空出位置
		/*给temp腾出位置,集体向后轮换*/
		/*每次都判断插入j这个空位是否合适*/
		for(j=i-1;(j>=0) && (records[j].key>temp.key);--j)
				records[j+1] = records[j];//j依然指向空位
		records[j+1] = temp;//填充空位,轮换结束
	}
}