折半插入排序就是查找操作使用折半查找的直接插入排序,除此之外它与直接插入排序基本一样。
算法属性:
时间复杂度为O(n^2)
空间复杂度为O(1)
稳定排序。
因为要进行折半查找, 所以只能用于顺序结构。
适合初始记录无序,n较大时的情况。
代码如下:
#include<stdio.h>
#define MAXSIZE 20
typedef int KeyType;
typedef int InfoType;
//记录类型
typedef struct {
KeyType key;
InfoType otherinfo;
}RedType;
//顺序表类型
typedef struct {
RedType r[MAXSIZE + 1]; //r[0]闲置或作哨兵
int length; //顺序表长度
}SqList;
//折半插入排序
void BInsertSort(SqList &L) {