宏定义如下:
#include<iostream> using namespace std; #define M 21 typedef int SqList[M];
一.直接插入排序
实现代码如下:
void InsertSort(SqList &L,int n)//直接插入排序 { for(int i = 2;i < n;++i) //从下标为2处开始处理 { if(L[i] <= L[i-1]) { L[0] = L[i]; //标兵位 for(int j = i-1;L[0] < L[j];--j)//向后移位 { L[j+1] = L[j]; } L[j+1] = L[0]; //赋值 } } }
测试函数实现如下:
void main() { SqList sq = {0,13,24,35,56,32,78,99};//有标兵位 for(int i = 1;i < 8; ++i) //打印sq { cout<<sq[i]<<" "; } cout<<endl; InsertSort(sq,8); //直接插入排序结果打印 for(i = 1;i < 8; ++i) { cout<<sq[i]<<" "; } cout<<endl; }
二.折半插入排序
实现代码如下:
void BInsertSort(SqList &L,int n)//折半插入排序 { for(int i = 2;i < n;++i) //从下标为2处开始处理 { L[0] = L[i]; //标兵位 int low = 1; //低位 int high = i-1; //高位 int mid; //中间值 while(low <= high) //比较条件 { mid = (low+high)/2; //mid值 if(L[0] <= L[mid]) //判断 { high = mid - 1; } else { low = mid + 1; } } for(int j = i-1;j >= high+1;--j)//向后移位 { L[j+1] = L[j]; } L[high+1] = L[0]; //赋值 } }
测试函数实现如下:
void main() { SqList sq = {0,13,24,35,56,32,78,99};//有标兵位 for(int i = 1;i < 8; ++i) //打印sq { cout<<sq[i]<<" "; } cout<<endl; BInsertSort(sq,8); //折半插入排序结果打印 for(i = 1;i < 8; ++i) { cout<<sq[i]<<" "; } cout<<endl; }
三.2—路插入排序
实现代码如下:
void TWayInsertSort(SqList &L,int n)//2—路插入排序 { SqList T; T[0] = L[0]; int first; int last; first = last = 0; for(int i = 1;i < n;++i) { if(L[i] < T[first]) { first = (first-1+n)%n; T[first] = L[i]; } else if(L[i] > T[last]) { last++; T[last] = L[i]; } else { last++; T[last] = T[last-1]; for(int j = last-1;L[i] < T[(j-1+n)%n];j = (j-1+n)%n) { T[j] = T[(j-1+n)%n]; } T[j] = L[i]; } } for(i = 0;i < n; ++i) { L[i] = T[first]; first = (first+1)%n; } }
测试函数实现如下:
void main() { SqList sq2 = {13,24,35,56,32,78,99}; //无标兵位 for(i = 0;i < 7; ++i) //打印sq2 { cout<<sq2[i]<<" "; } cout<<endl; TWayInsertSort(sq2,7); //2—路插入排序结果打印 for(i = 0;i < 7; ++i) { cout<<sq2[i]<<" "; } cout<<endl; }
四.希尔排序
实现代码如下:
void ShellInsert(SqList &L,int n,int dk)//希尔插入 { for(int i = dk+1;i < n;++i) //按增量值插入 { if(L[i] < L[i-dk]) //比较大小 { L[0] = L[i]; //空闲单元 for(int j = i - dk;j>1&&L[0]<L[j];j -= dk)//组内插入排序 { L[j+dk] = L[j]; } L[j+dk] = L[0]; //赋值 } } } void ShellSort(SqList &L,int n,int dlta[],int t)//希尔排序 { for(int k = 0;k < t;++k) //按照增量数组值重复插入排序 { ShellInsert(L,n,dlta[k]); } }
测试函数实现如下:
void main() { SqList sq = {0,13,24,35,56,32,78,99};//有标兵位 for(int i = 1;i < 8; ++i) //打印sq { cout<<sq[i]<<" "; } cout<<endl; int dlta[] = {5,3,2,1}; //增量数组 ShellSort(sq,8,dlta,sizeof(dlta)/sizeof(int));//希尔排序结果打印 for(i = 1;i < 8; ++i) { cout<<sq[i]<<" "; } cout<<endl; }
如果有没有考虑到的地方希望大家指出,谢谢~
完整的函数及测试如下:
#include<iostream> using namespace std; #define M 21 typedef int SqList[M]; void InsertSort(SqList &L,int n)//直接插入排序 { for(int i = 2;i < n;++i) //从下标为2处开始处理 { if(L[i] <= L[i-1]) { L[0] = L[i]; //标兵位 for(int j = i-1;L[0] < L[j];--j)//向后移位 { L[j+1] = L[j]; } L[j+1] = L[0]; //赋值 } } } void BInsertSort(SqList &L,int n)//折半插入排序 { for(int i = 2;i < n;++i) //从下标为2处开始处理 { L[0] = L[i]; //标兵位 int low = 1; //低位 int high = i-1; //高位 int mid; //中间值 while(low <= high) //比较条件 { mid = (low+high)/2; //mid值 if(L[0] <= L[mid]) //判断 { high = mid - 1; } else { low = mid + 1; } } for(int j = i-1;j >= high+1;--j)//向后移位 { L[j+1] = L[j]; } L[high+1] = L[0]; //赋值 } } void TWayInsertSort(SqList &L,int n)//2—路插入排序 { SqList T; T[0] = L[0]; int first; int last; first = last = 0; for(int i = 1;i < n;++i) { if(L[i] < T[first]) { first = (first-1+n)%n; T[first] = L[i]; } else if(L[i] > T[last]) { last++; T[last] = L[i]; } else { last++; T[last] = T[last-1]; for(int j = last-1;L[i] < T[(j-1+n)%n];j = (j-1+n)%n) { T[j] = T[(j-1+n)%n]; } T[j] = L[i]; } } for(i = 0;i < n; ++i) { L[i] = T[first]; first = (first+1)%n; } } void ShellInsert(SqList &L,int n,int dk)//希尔插入 { for(int i = dk+1;i < n;++i) //按增量值插入 { if(L[i] < L[i-dk]) //比较大小 { L[0] = L[i]; //空闲单元 for(int j = i - dk;j>1&&L[0]<L[j];j -= dk)//组内插入排序 { L[j+dk] = L[j]; } L[j+dk] = L[0]; //赋值 } } } void ShellSort(SqList &L,int n,int dlta[],int t)//希尔排序 { for(int k = 0;k < t;++k) //按照增量数组值重复插入排序 { ShellInsert(L,n,dlta[k]); } } void main() { SqList sq = {0,13,24,35,56,32,78,99};//有标兵位 for(int i = 1;i < 8; ++i) //打印sq { cout<<sq[i]<<" "; } cout<<endl; SqList sq2 = {13,24,35,56,32,78,99}; //无标兵位 for(i = 0;i < 7; ++i) //打印sq2 { cout<<sq2[i]<<" "; } cout<<endl; InsertSort(sq,8); //直接插入排序结果打印 for(i = 1;i < 8; ++i) { cout<<sq[i]<<" "; } cout<<endl; BInsertSort(sq,8); //折半插入排序结果打印 for(i = 1;i < 8; ++i) { cout<<sq[i]<<" "; } cout<<endl; TWayInsertSort(sq2,7); //2—路插入排序结果打印 for(i = 0;i < 7; ++i) { cout<<sq2[i]<<" "; } cout<<endl; int dlta[] = {5,3,2,1}; //增量数组 ShellSort(sq,8,dlta,sizeof(dlta)/sizeof(int));//希尔排序结果打印 for(i = 1;i < 8; ++i) { cout<<sq[i]<<" "; } cout<<endl; }
时间: 2024-10-09 17:42:17