1.希尔排序
希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序;随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,算法便终止。
//希尔排序
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef struct
{
int key;
char *otherinfo;
}ElemType;
typedef struct
{
ElemType *r;
int length;
}SqList;
void ShellInsert(SqList &L,int dk)
{
int i,j;
for(i=dk+1;i<=L.length;i++)
{
if(L.r[i].key<L.r[i-dk].key)
{
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];
}
}
}
void ShellSort(SqList &L,int dt[],int t)
{
int k;
for(k=0;k<t;k++)
{
ShellInsert(L,dt[k]);
}
}
int main()
{
SqList L;
L.r=new ElemType[100];
L.length=0;
printf("请输入数据个数:");
int nn;
scanf("%d",&nn);
printf("请输入待排序数据:\n");
for(int i=1;i<=nn;i++)
{
scanf("%d",&L.r[i].key);
L.length++;
}
int i,t;
int *dt=new int[1000];//增量数组
printf("输入增量的个数:");
int yy;
scanf("%d",&yy);
for(i=0;i<yy;i++)
{
scanf("%d",&dt[i]);
}
ShellSort(L,dt,yy);
printf("排序后的结果:");
for(i=1;i<=nn;i++)
{
printf("%d ",L.r[i].key);
}
printf("\n");
return 0;
}
<strong><span style="font-size:24px;">2.快速排序</span></strong>
快速排序(Quicksort)是对
冒泡排序的一种改进。
快速排序由C. A. R. Hoare在1962年提出。它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以
递归进行,以此达到整个数据变成有序
序列。
//快速排序
#include <iostream>
using namespace std;
#define MAXSIZE 20 //顺序表的最大长度
typedef struct
{
int key;
char *otherinfo;
}ElemType;
//顺序表的存储结构
typedef struct
{
ElemType *r; //存储空间的基地址
int length; //顺序表长度
}SqList; //顺序表类型
int Partition(SqList &L,int low,int high)
{
//对顺序表L中的子表r[low..high]进行一趟排序,返回枢轴位置
int pivotkey;
L.r[0]=L.r[low]; //用子表的第一个记录做枢轴记录
pivotkey=L.r[low].key; //枢轴记录关键字保存在pivotkey中
while(low<high)
{ //从表的两端交替地向中间扫描
while(low<high&&L.r[high].key>=pivotkey) --high;
L.r[low]=L.r[high]; //将比枢轴记录小的记录移到低端
while(low<high&&L.r[low].key<=pivotkey) ++low;
L.r[high]=L.r[low]; //将比枢轴记录大的记录移到高端
}//while
L.r[low]=L.r[0]; //枢轴记录到位
return low; //返回枢轴位置
}//Partition
void QSort(SqList &L,int low,int high)
{ //调用前置初值:low=1; high=L.length;
//对顺序表L中的子序列L.r[low..high]做快速排序
int pivotloc;
if(low<high)
{ //长度大于1
pivotloc=Partition(L,low,high); //将L.r[low..high]一分为二,pivotloc是枢轴位置
QSort(L,low,pivotloc-1); //对左子表递归排序
QSort(L,pivotloc+1,high); //对右子表递归排序
}
} //QSort
void QuickSort(SqList &L)
{
//对顺序表L做快速排序
QSort(L,1,L.length);
} //QuickSort
void Create_Sq(SqList &L)
{
int i,n;
cout<<"请输入数据个数,不超过"<<MAXSIZE<<"个。"<<endl;
cin>>n; //输入个数
cout<<"请输入待排序的数据:\n";
while(n>MAXSIZE)
{
cout<<"个数超过上限,不能超过"<<MAXSIZE<<",请重新输入"<<endl;
cin>>n;
}
for(i=1;i<=n;i++)
{
cin>>L.r[i].key;
L.length++;
}
}
void show(SqList L)
{
int i;
for(i=1;i<=L.length;i++)
cout<<L.r[i].key<<endl;
}
int main()
{
SqList L;
L.r=new ElemType[MAXSIZE+1];
L.length=0;
Create_Sq(L);
QuickSort(L);
cout<<"排序后的结果为:"<<endl;
show(L);
}
3.堆排序
堆排序(Heapsort)是指利用堆积树(堆)这种
数据结构
所设计的一种
排序算法
,它是选择排序的一种。可以利用
数组
的特点快速定位指定索引的元素。堆分为大根堆和小根堆,是完全二叉树
。大根堆的要求是每个节点的值都不大于其父节点的值,即A[PARENT[i]] >= A[i]。
在数组的非降序排序中,需要使用的就是大根堆,因为根据大根堆的要求可知,最大的值一定在堆顶。
//堆排序
#include <iostream>
using namespace std;
#define MAXSIZE 20 //顺序表的最大长度
typedef struct
{
int key;
char *otherinfo;
}ElemType;
typedef struct
{
ElemType *r; //存储空间的基地址
int length; //顺序表长度
}SqList; //顺序表类型
void HeapAdjust(SqList &L,int s,int m)
{
//假设r[s+1..m]已经是堆,将r[s..m]调整为以r[s]为根的大根堆
ElemType rc;
int j;
rc=L.r[s];
for(j=2*s;j<=m;j*=2)
{ //沿key较大的孩子结点向下筛选
if(j<m&&L.r[j].key<L.r[j+1].key) ++j; //j为key较大的记录的下标
if(rc.key>=L.r[j].key) break; //rc应插入在位置s上
L.r[s]=L.r[j]; s=j;
}
L.r[s]=rc; //插入
} //HeapAdjust
void Create_Sq(SqList &L)
{
int i,n;
cout<<"请输入数据个数,不超过"<<MAXSIZE<<"个。"<<endl;
cin>>n; //输入个数
cout<<"请输入待排序的数据:\n";
while(n>MAXSIZE)
{
cout<<"个数超过上限,不能超过"<<MAXSIZE<<",请重新输入"<<endl;
cin>>n;
}
for(i=1;i<=n;i++)
{
cin>>L.r[i].key;
L.length++;
}
}
void CreatHeap(SqList &L)
{
//把无序序列L.r[1..n]建成大根堆
int i,n;
n=L.length;
for(i=n/2;i>0;--i) //反复调用HeapAdjust
HeapAdjust(L,i,n);
} //CreatHeap
void HeapSort(SqList &L)
{
//对顺序表L进行堆排序
int i;
ElemType x;
CreatHeap(L); //把无序序列L.r[1..L.length]建成大根堆
for(i=L.length;i>1;--i)
{
x=L.r[1]; //将堆顶记录和当前未经排序子序列L.r[1..i]中最后一个记录互换
L.r[1]=L.r[i];
L.r[i]=x;
HeapAdjust(L,1,i-1); //将L.r[1..i-1]重新调整为大根堆 **
}
}
void show(SqList L)
{
int i;
for(i=1;i<=L.length;i++)
cout<<L.r[i].key<<endl;
}
int main()
{
SqList L;
L.r=new ElemType[MAXSIZE+1];
L.length=0;
Create_Sq(L);
HeapSort(L);
cout<<"排序后的结果为:"<<endl;
show(L);
}