您的位置:首页 > 其它

第十六周上机实践—项目1(2)—验证算法 希尔排序 快速排序

2015-12-14 16:28 148 查看
/*
*Copyright(c) 2015,烟台大学计算机学院
*All rights reserved.
*文件名称:test.cpp
*作者:林莉
*完成日期:2015年12月14日
*版本:v1.0
*
*问题描述:用序列{57, 40, 38, 11, 13, 34, 48, 75, 6, 19, 9, 7}作为测试数据,运行并本周视频中所讲过的算法对应 程
序,观察运行结果并深刻领会算法的思路和实现方法:希尔排序;快速排序;

*输入描述:无
*输出描述:所得结果。
*/

1.希尔排序

#include <stdio.h>
#define MaxSize 20
typedef int KeyType;    //定义关键字类型
typedef char InfoType[10];
typedef struct          //记录类型
{
KeyType key;        //关键字项
InfoType data;      //其他数据项,类型为InfoType
} RecType;              //排序的记录类型定义

void ShellSort(RecType R[],int n)   //希尔排序算法
{
int i,j,gap;
RecType tmp;
gap=n/2;                //增量置初值
while (gap>0)
{
for (i=gap; i<n; i++) //对所有相隔gap位置的所有元素组进行排序
{
tmp=R[i];
j=i-gap;
while (j>=0 && tmp.key<R[j].key)//对相隔gap位置的元素组进行排序
{
R[j+gap]=R[j];
j=j-gap;
}
R[j+gap]=tmp;
j=j-gap;
}
gap=gap/2;  //减小增量
}
}

int main()
{
int i,n=11;
RecType R[MaxSize];
KeyType a[]= {16,25,12,30,47,11,23,36,9,18,31};
for (i=0; i<n; i++)
R[i].key=a[i];
printf("排序前:");
for (i=0; i<n; i++)
printf("%d ",R[i].key);
printf("\n");
ShellSort(R,n);
printf("排序后:");
for (i=0; i<n; i++)
printf("%d ",R[i].key);
printf("\n");
return 0;
}

运行结果:



排序中输出每一趟的中间结果

#include <stdio.h>
#define MaxSize 20
typedef int KeyType;    //定义关键字类型
typedef char InfoType[10];
typedef struct          //记录类型
{
KeyType key;        //关键字项
InfoType data;      //其他数据项,类型为InfoType
} RecType;              //排序的记录类型定义

void ShellSort(RecType R[],int n)   //希尔排序算法
{
int i,j,gap,k;
RecType tmp;
gap=n/2;                //增量置初值
while (gap>0)
{
for (i=gap; i<n; i++) //对所有相隔gap位置的所有元素组进行排序
{
tmp=R[i];
j=i-gap;
while (j>=0 && tmp.key<R[j].key)//对相隔gap位置的元素组进行排序
{
R[j+gap]=R[j];
j=j-gap;
}
R[j+gap]=tmp;
j=j-gap;
}
printf("gap=%d:",gap);
for (k=0; k<n; k++)
printf("%d ",R[k].key);
printf("\n");
gap=gap/2;  //减小增量
}
}

int main()
{
int i,n=11;
RecType R[MaxSize];
KeyType a[]= {16,25,12,30,47,11,23,36,9,18,31};
for (i=0; i<n; i++)
R[i].key=a[i];
printf("排序前:");
for (i=0; i<n; i++)
printf("%d ",R[i].key);
printf("\n");
ShellSort(R,n);
printf("排序后:");
for (i=0; i<n; i++)
printf("%d ",R[i].key);
printf("\n");
return 0;
}

运行结果:



2.快速排序

(1)以第1个元素作为基准

#include <stdio.h>
#define MaxSize 20
typedef int KeyType;    //定义关键字类型
typedef char InfoType[10];
typedef struct          //记录类型
{
KeyType key;        //关键字项
InfoType data;      //其他数据项,类型为InfoType
} RecType;              //排序的记录类型定义
void QuickSort(RecType R[],int s,int t) //对R[s]至R[t]的元素进行快速排序
{
int i=s,j=t;
RecType tmp;
if (s<t)                //区间内至少存在两个元素的情况
{
tmp=R[s];           //用区间的第1个记录作为基准
while (i!=j)        //从区间两端交替向中间扫描,直至i=j为止
{
while (j>i && R[j].key>=tmp.key)
j--;        //从右向左扫描,找第1个小于tmp.key的R[j]
R[i]=R[j];      //找到这样的R[j],R[i]"R[j]交换
while (i<j && R[i].key<=tmp.key)
i++;        //从左向右扫描,找第1个大于tmp.key的记录R[i]
R[j]=R[i];      //找到这样的R[i],R[i]"R[j]交换
}
R[i]=tmp;
QuickSort(R,s,i-1);     //对左区间递归排序
QuickSort(R,i+1,t);     //对右区间递归排序
}
}
int main()
{
int i,n=10;
RecType R[MaxSize];
KeyType a[]= {57,40,38,11,13,34,48,75,6,19,9,7};
for (i=0; i<n; i++)
R[i].key=a[i];
printf("排序前:");
for (i=0; i<n; i++)
printf("%d ",R[i].key);
printf("\n");
QuickSort(R,0,n-1);
printf("排序后:");
for (i=0; i<n; i++)
printf("%d ",R[i].key);
printf("\n");
return 0;
}


运行结果:



(2)以中间位置的元素作为基准

#include <stdio.h>
#define MaxSize 20
typedef int KeyType;    //定义关键字类型
typedef char InfoType[10];
typedef struct          //记录类型
{
KeyType key;        //关键字项
InfoType data;      //其他数据项,类型为InfoType
} RecType;              //排序的记录类型定义
void QuickSort1(RecType R[],int s,int t) //对R[s]至R[t]的元素进行快速排序
{
int i=s,j=t;
KeyType pivot;
RecType tmp;
pivot = R[(s+t)/2].key; //用区间的中间位置的元素作为关键字
if (s<t)                //区间内至少存在两个元素的情况
{
while (i!=j)        //从区间两端交替向中间扫描,直至i=j为止
{
while (j>i && R[j].key>pivot)
j--;        //从右向左扫描,找第1个小于基准的R[j]
while (i<j && R[i].key<pivot)
i++;        //从左向右扫描,找第1个大于基准记录R[i]
if(i<j)        //将前后的两个失序元素进行交换
{
tmp=R[i];
R[i]=R[j];
R[j]=tmp;
}
}
QuickSort1(R,s,i-1);        //对左区间递归排序
QuickSort1(R,j+1,t);        //对右区间递归排序
}
}
int main()
{
int i,n=10;
RecType R[MaxSize];
KeyType a[]= {57,40,38,11,13,34,48,75,6,19,9,7};
for (i=0; i<n; i++)
R[i].key=a[i];
printf("排序前:");
for (i=0; i<n; i++)
printf("%d ",R[i].key);
printf("\n");
QuickSort1(R,0,n-1);
printf("排序后:");
for (i=0; i<n; i++)
printf("%d ",R[i].key);
printf("\n");
return 0;
}


运行结果:



 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: