接下来的几篇博客都是关于排序的;主要有插入类排序;交换类排序;选择类排序;
插入类排序主要有直接插入如排序(straight insertion sort);折半插入排序(bianry insertion sort); Shell sort;
交换类排序主要有冒泡排序(bubble sort); 快速排序(quick sort);
选择类排序主要有;简单选择排序(simple selection sort); 堆排序(heap sort);
除此之外还有归并排序(merge sort); 基数排序等;
本篇博客是关于Shell排序的;
排序直接的数据结构介绍;
所有的排序均以线性表存储;所有的排序算法避免不了交换;交换就要用到临时变量;故将线性表中编号为0的元素不存储任何有效元素;仅仅当做临时变量或者记录的作用来使用;对于有些算法我会给出局部变量做临时变量的算法;若对线性表不是很了解;参见前面关于线性表的博客;
头文件(sort.h);
# ifndef _SORT_
typedef int KeyType;
typedef struct
{
KeyType key;
}DataType;
# endif
头文件(SeqList.h);
typedef struct
{
DataType data[MAXSIZE];
int length;
}SeqList, * PSeqList;
//线性表排序基本准备;
PSeqList Init_SeqList(void);
bool Full_SeqList(PSeqList p);
int Push_SeqList(PSeqList p, KeyType keyValue);
void Traversal_SeqList(PSeqList p);
ostream & operator<<(ostream & os, DataType dataValue);
//基于线性表的排序方法;默认从小到大;
void StraightInsertSort(PSeqList p);
void BinaryInsertSort(PSeqList p);
void ShellInsert(PSeqList p, int gap);
void ShellSort(PSeqList p, int * gaps, int len);
实现文件(SeqList.cpp);
# include "SeqList.h"
PSeqList Init_SeqList(void)
{
PSeqList p = (PSeqList)malloc(sizeof(SeqList));
if (NULL != p)
{
p->length = 0;
return p;
}
else
{
cout << "Memory allocate is error! " << endl;
system("pause");
exit(0);
}
}
bool Full_SeqList(PSeqList p)
{
if (p->length >= MAXSIZE)
{
return true;
}
else
{
return false;
}
}
int Push_SeqList(PSeqList p, KeyType keyValue)
{
if (Full_SeqList(p))
{
cout << "SeqList is full! " << endl;
return -1;
}
p->data[p->length].key = keyValue;
p->length++;
return 0;
}
void Traversal_SeqList(PSeqList p)
{
for (int i = 0; i < p->length; i++)
{
cout << p->data[i] << " ";
}
cout << endl;
return;
}
ostream & operator<<(ostream & os, DataType dataValue)
{
cout << dataValue.key;
return os;
}
//基于线性表的排序方法;默认从小到大;
//直接插入排序;
void StraightInsertSort(PSeqList p)
{
int i = 0;
int j = 0;
for (i = 2; i < p->length; i++)
{
//复制到前哨站;
p->data[0] = p->data[i];
j = i - 1;
while (p->data[0].key < p->data[j].key)//必须是大于;不然不是稳定排序;
{
//移动元素;
p->data[j + 1] = p->data[j];
j--;
}
//元素最终移入正确位置;
p->data[j + 1] = p->data[0];
}
return;
}
//折半插入排序;
void BinaryInsertSort(PSeqList p)
{
int i = 0;
int j = 0;
int low = 0;
int mid = 0;
int high = 0;
for (i = 2; i < p->length; i++)
{
//复制到前哨站;
p->data[0] = p->data[i];
//查找插入位置;
low = 1;
high = i - 1;
while (low <= high)
{
mid = (low + high) / 2;
if (p->data[0].key >= p->data[mid].key)//要么是大于等于要么是小于;即是相反;不然不是稳定排序;
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
//移动元素腾出插入位置;这里high + 1为最终插入位置;
for (j = i - 1; j >= high + 1; j--)
{
p->data[j + 1] = p->data[j];
}
//元素最终移入正确位置;
p->data[j + 1] = p->data[0];
}
return;
}
//Shell插入;
void ShellInsert(PSeqList p, int gap)
{
int i = 0;
int j = 0;
for (i = gap + i; i < p->length; i++)
{
if (p->data[i].key < p->data[i - gap].key)
{
p->data[0] = p->data[i];
for (j = i - gap; (j > 0) && (p->data[0].key < p->data[j].key); j = j - gap)
{
p->data[j + gap] = p->data[j];
}
p->data[j + gap] = p->data[0];
}
}
return;
}
//Shell排序;
void ShellSort(PSeqList p, int * gaps, int len)
{
int i = 0;
for (i = 0; i < len; i++)
{
ShellInsert(p, gaps[i]);
}
return;
}
主函数(Main.cpp);
# include "SeqList.h"
int main(int argc, char ** argv)
{
cout << endl << endl << "----------------Shell test---------------" << endl;
PSeqList pp = Init_SeqList();
Push_SeqList(pp, 1);
Push_SeqList(pp, 39);
Push_SeqList(pp, 80);
Push_SeqList(pp, 76);
Push_SeqList(pp, 41);
Push_SeqList(pp, 13);
Push_SeqList(pp, 29);
Push_SeqList(pp, 50);
Push_SeqList(pp, 78);
Push_SeqList(pp, 30);
Push_SeqList(pp, 11);
Push_SeqList(pp, 100);
Push_SeqList(pp, 7);
Push_SeqList(pp, 41);
Push_SeqList(pp, 86);
Traversal_SeqList(pp);
cout << endl;
int gaps[] = { 5, 3, 1 };
ShellAllSort(pp, gaps, 3);
Traversal_SeqList(pp);
cout << endl;
system("pause");
return 0;
}
上面的算法只是对{ 1..........N}的元素进行排序;下面给出对{ 0.......N}的元素进行排序的算法;
分析;前面没有排序0号元素是因为0号元素被当做临时变量使用了,现在自定义一个临时变量代替它就可以了;
void ShellAllInsert(PSeqList p, int gap)
{
int i = 0;
int j = 0;
DataType tem;
for (i = gap; i < p->length; i++)
{
if (p->data[i].key < p->data[i - gap].key)
{
tem = p->data[i];
for (j = i - gap; j >= 0 && tem.key < p->data[j].key; j = j - gap)
{
p->data[j + gap] = p->data[j];
}
p->data[j + gap] = tem;
}
}
return;
}
void ShellAllSort(PSeqList p, int * gaps, int len)
{
int i = 0;
for (i = 0; i < len; i++)
{
ShellAllInsert(p, gaps[i]);
}
return;
}
来源:https://blog.csdn.net/qinjiawei_156558/article/details/98885098