#define MAXSIZE 20 #define EQ(a, b) ((a) == (b)) #define LT(a, b) ((a) < (b)) #define LQ(a, b) ((a) <= (b)) typedef int KeyType; typedef int InfoType; typedef struct...{ KeyType key; InfoType otherinfo; }RedType; typedef struct...{ //r[0] is the sentinel, not used. RedType r[MAXSIZE +1]; int length; }SqList;
源文件:
#include "stdio.h" #include "sort.h" void init(SqList &s, int w[], int n)...{ s.length = n; for(int i =1; i <= n; i++)...{ s.r[i].key = w[i -1]; } } void show(SqList s)...{ for(int i =1; i <= s.length; i++)...{ printf("%d ", s.r[i]); } printf(""); } void insert(SqList &s)...{ for(int i =2; i <= s.length; i++)...{ if(LT(s.r[i].key, s.r[i -1].key))...{ //set the sentinel s.r[0] = s.r[i]; s.r[i] = s.r[i -1]; for(int j = i -2; LT(s.r[0].key, s.r[j].key); j--)...{ s.r[j +1] = s.r[j]; } s.r[j +1] = s.r[0]; } } } void main()...{ int w[] =...{49, 38, 65, 97, 76, 13, 27, 49}; int n =8; SqList s; init(s, w, n); insert(s); show(s); }
执行结果:
1327384949657697 Press any key to continue
(2)减少比较次数-折半插入
源文件添加:
//use the binary search method to find the location to insert. void binaryInsert(SqList &s)...{ for(int i =2; i <= s.length; i++)...{ //set the sentinel s.r[0] = s.r[i]; //binary search the location to insert int low =1, high = i -1; int m; while(low <= high)...{ m = (low + high) /2; if(LT(s.r[0].key, s.r[m].key))...{ high = m -1; }else...{ low = m +1; } } for(int j = i -1; j >= high +1; j--)...{ s.r[j +1] = s.r[j]; } s.r[high +1] = s.r[0]; } }
折半插入仅减少了比较次数,而记录的移动次数没变。
(3)减少移动次数-表插入排序
头文件:
#define SIZE 100 #define EQ(a, b) ((a) == (b)) #define LT(a, b) ((a) < (b)) #define LQ(a, b) ((a) <= (b)) typedef int KeyType; typedef int InfoType; typedef struct...{ KeyType key; InfoType otherinfo; }RedType; typedef struct...{ RedType rc; int next; }SLNode; typedef struct...{ //r[0] is head of static list SLNode r[SIZE]; int length; }SLinkListType;
源文件:
#include "stdio.h" #include "SLLInsert.h" void init(SLinkListType &s, int w[], int n)...{ s.length = n; for(int i =1; i <= n; i++)...{ s.r[i].rc.key = w[i -1]; //point to the head, to use r[o] as a sentinal s.r[i].next =0; } s.r[0].next =1; } void show(SLinkListType s)...{ for(int i =0; i <= s.length; i++)...{ printf("%d: %d(%d) ", i, s.r[i].rc.key, s.r[i].next); } printf(""); } void insert(SLinkListType &s)...{ int parent, next; for(int i =2; i <= s.length; i++)...{ s.r[0].rc.key = s.r[i].rc.key; parent =0; next = s.r[parent].next; while(LT(s.r[next].rc.key, s.r[0].rc.key))...{ parent = next; next = s.r[next].next; } //insert s.r[i].next = next; s.r[parent].next = i; } } void main()...{ int w[] =...{49, 38, 65, 97, 76, 13, 27, 49}; int n =8; SLinkListType s; init(s, w, n); insert(s); show(s); }