1. 直接插入排序
//直接插入排序
#include<iostream>
using namespace std;
#define MAXSIZE 20
typedef struct {
int key;
char* otherinfo;
}ElemType;
//顺序表的存储结构
typedef struct {
ElemType* r;
int length;
}SqList;
void InsertSort(SqList& L)
{
int j;
for (int i = 2; i <= L.length; i++)
{
if (L.r[i].key < L.r[i - 1].key)
{
L.r[0] = L.r[i]; //将待插入的记录暂存到监视哨中
L.r[i] = L.r[i - 1]; //r[i-1]后移
for (j = i-2; L.r[0].key < L.r[j].key; j--) //从后向前寻找插入位置
{
L.r[j + 1] = L.r[j]; //记录逐个后移,直到找到插入位置
}
L.r[j + 1] = L.r[0]; //将r[0]即r[i],插入到正确位置
}
}
}
void Create_Sq(SqList& L)
{
int n;
cout << "请输入数据个数,不超过" << MAXSIZE << "个" << endl;
cin >> n;
while (n > MAXSIZE)
{
cout << "个数超过上限,不能超过" << MAXSIZE << ",请重新输入" << endl;
cin >> n;
}
cout << "请输入待排序的数据:" << endl;
for (int i = 1; i <= n; i++)
{
cin >> L.r[i].key;
L.length++;
}
}
void show(SqList L)
{
for (int i = 1; i <= L.length; i++)
{
cout << L.r[i].key << " ";
}
cout << endl;
}
int main()
{
SqList L;
L.r = new ElemType[MAXSIZE + 1];
L.length = 0;
Create_Sq(L);
InsertSort(L);
cout << "排序后的结果为:" << endl;
show(L);
return 0;
}
在具体实现r[i]向前面的有序序列插入时,有两种方法:一种是将r[i]与r[1],r[2],…,r[i-1]从前向后顺序比较;另一种是将r[i]与r[i-1],r[i-2],…,r[1]从后向前顺序比较。这里采用后一种方法,和顺序查找类似,为了在查找插入位置的过程中避免数组下标出界,在r[0]处设置监视哨。在自i-1起往前查找插入位置的过程中,可以同时向后移记录。
2. 折半插入排序
#include<iostream>
using namespace std;
#define MAXSIZE 20
typedef struct {
int key;
char* otherinfo;
}ElemType;
//顺序表的存储结构
typedef struct {
ElemType* r;
int length;
}SqList;
void BInsertSort(SqList& L)
{
int low, high,m;
for (int i = 2; i <= L.length; i++)
{
L.r[0] = L.r[i]; //将待插入的巨鹿暂存到监视哨中
low = 1; //置查找区间初值
high = i - 1;
while (low <= high)
{
m = (low + high) / 2;
if (L.r[0].key < L.r[m].key)
{
high = m - 1;
}
else
{
low = m + 1;
}
}
for (int j = i - 1; j >= high + 1; j--)
{
L.r[j + 1] = L.r[j];
}
L.r[high + 1] = L.r[0]; //将r[0]即原r[i],插入到正确位置
}
}
void Create_Sq(SqList& L)
{
int n;
cout << "请输入数据个数,不超过" << MAXSIZE << "个" << endl;
cin >> n;
while (n > MAXSIZE)
{
cout << "个数超过上限,不能超过" << MAXSIZE << ",请重新输入" << endl;
cin >> n;
}
cout << "请输入待排序的数据:" << endl;
for (int i = 1; i <= n; i++)
{
cin >> L.r[i].key;
L.length++;
}
}
void show(SqList L)
{
for (int i = 1; i <= L.length; i++)
{
cout << L.r[i].key << " ";
}
cout << endl;
}
int main()
{
SqList L;
L.r = new ElemType[MAXSIZE + 1];
L.length = 0;
Create_Sq(L);
BInsertSort(L);
cout << "排序后的结果为:";
show(L);
return 0;
}
依次将元素记录后移,找到最终插入的正确位置
3. 希尔排序
#include<iostream>
using namespace std;
#define MAXSIZE 20
typedef struct {
int key;
char* otherinfo;
}ElemType;
//顺序表的存储结构
typedef struct {
ElemType* r;
int length;
}SqList;
void ShellInsert(SqList& L, int dk) //对顺序表L做一趟增量是dk的希尔插入排序
{
int j;
for (int i = dk + 1; i <= L.length; i++)
{
if (L.r[i].key < L.r[i - dk].key) //需将L.r[i]插入有序增量子表
{
L.r[0] = L.r[i];
for (j = i - dk; j > 0 && L.r[0].key < L.r[j].key; j -= dk)
L.r[j + dk] = L.r[j]; //记录后移,直到找到插入位置
L.r[j + dk] = L.r[0]; //将r[0]即原r[i],插入到正确位置
}
}
}
void ShellSort(SqList& L, int dt[], int t) //按增量序列dt[0..t-1]对顺序表L作t趟希尔插入排序
{
for (int k = 0; k < t; k++)
{
ShellInsert(L, dt[k]);
}
}
void Create_Sq(SqList& L)
{
int n;
cout << "请输入数据个数,不超过" << MAXSIZE << "个" << endl;
cin >> n;
while (n > MAXSIZE)
{
cout << "个数超过上限,不能超过" << MAXSIZE << ",请重新输入" << endl;
cin >> n;
}
cout << "请输入待排序的数据:" << endl;
for (int i = 1; i <= n; i++)
{
cin >> L.r[i].key;
L.length++;
}
}
void show(SqList L)
{
for (int i = 1; i <= L.length; i++)
{
cout << L.r[i].key << " ";
}
cout << endl;
}
int main()
{
SqList L;
L.r = new ElemType[MAXSIZE + 1];
L.length = 0;
Create_Sq(L);
int t;
int* dt = new int[MAXSIZE];
cout << "请输入增量个数:";
cin >> t;
for (int i = 0; i < t; i++)
{
cout << "第" << i + 1 << "个增量:";
cin >> dt[i];
}
ShellSort(L, dt, t);
cout << "排序后的结果为:";
show(L);
return 0;
}
1.第一趟取增量d1=5,所有间隔为5的记录分在同一组,全部记录分成5组,在各个组中分别进行直接插入排序
2.第二趟取增量d2=3,所有间隔为3的记录分在同一组,全部记录分成3组,在各个组中分别进行直接插入排序
3.第三趟取增量d3=1,对整个序列进行一趟直接插入排序,排序完成。