插入排序包括直接插入排序和改进的折半插入排序、2-路插入排序、表插入排序和希尔排序。
折半插入排序可减少关键字间的比较次数,记录的移动次数不变;
2-路插入排序目的是减少排序过程中的移动次数,为此需要N个记录的辅助空间;
表插入排序则可以完全避免记录的移动,但因比较次数一样,时间复杂度仍不变;
希尔排序又称“缩小增量排序”,其基本思想为:先将整个待排记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录基本有序时,再对全体记录进行一次直接插入排序。
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MAXSIZE 20
typedef int KeyType;
typedef int InfoType;
typedef struct
{
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct
{
RedType r[MAXSIZE+1];
int length;
}SqList;
/*静态链表数据结构*/
#define SIZE 100
typedef struct
{
KeyType rc;
int next;
}SLNode;
typedef struct
{
SLNode r[SIZE];
int length;
}SLinkList;
void InsertSort(SqList *L);
int LT(KeyType a,KeyType b);
void BInsertSort(SqList *L);
void BnInsertSort(SqList *L);
void SLListSort(SLinkList *SL);
void Arrange(SLinkList *SL);
void ShellInsert(SqList *L,int dk);
void ShellSort(SqList *L,int dlta[],int t);
int main()
{
SqList *L,*L1,*L2,*L3,*L4;
L=(SqList *)malloc(sizeof(SqList));
L1=(SqList *)malloc(sizeof(SqList));
L2=(SqList *)malloc(sizeof(SqList));
L3=(SqList *)malloc(sizeof(SqList));
L4=(SqList *)malloc(sizeof(SqList));
SLinkList *SL;
SL=(SLinkList *)malloc(sizeof(SLinkList));
int i;
printf("需排序的项数为:");
scanf("%d",&L->length);
SL->length=L->length; /* 为静态链表赋长度*/
for(i=0;i<L->length;i++)
{
printf("please input the number:");
scanf("%d",&L->r[i+1].key);
SL->r[i+1].rc&