/*
烟台大学计算机与控制工程学院
姓名:范宝磊
日期:2017.12.10
文件名称:y5962017
问题描述:用序列{57, 40, 38, 11, 13, 34, 48, 75, 6, 19, 9, 7}作为测试数据,
运行并本周视频中所讲过的算法对应 程序,观察运行结果并深刻领会算法的思路和实现方法:
(3)冒泡排序;(4)快速排序;
(5)直接选择排序;(6)堆排序;
(7)归并排序;(8)基数排序。
输入:无
输出:排序后的序列
*/
//冒泡:
#include
#define MaxSize 20
typedef int KeyType; //定义关键字类型
typedef char InfoType[10];
typedef struct //记录类型
{
KeyType key; //关键字项
InfoType data; //其他数据项,类型为InfoType
} RecType; //排序的记录类型定义
void BubbleSort(RecType R[],int n)
{
int i,j,k;
RecType tmp;
for (i=0; ii; j--) //比较,找出本趟最小关键字的记录
if (R[j].key
#define MaxSize 20
typedef int KeyType; //定义关键字类型
typedef char InfoType[10];
typedef struct //记录类型
{
KeyType key; //关键字项
InfoType data; //其他数据项,类型为InfoType
} RecType; //排序的记录类型定义
void BubbleSort(RecType R[],int n)
{
int i,j,k;
RecType tmp;
for (i=0; ii; j--) //比较,找出本趟最小关键字的记录
if (R[j].key
#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 (si && R[j].key>=tmp.key)
j--; //从右向左扫描,找第1个小于tmp.key的R[j]
R[i]=R[j]; //找到这样的R[j],R[i]"R[j]交换
while (i
void SelectionSort(int *num,int n)
{
int i=0;
int min=0;
int j=0;
int tmp=0;
for(i=0;inum[j])
{
min=j;
}
}
if(min!=i)//如果最小元素不是无序组起始位置元素,则与起始元素交换位置
{
tmp=num[min];
num[min]=num[i];
num[i]=tmp;
}
}
}
int main()
{
int num[12] = {57,40,38,11,13,34,48,75,6,19,9,7};
int i=0;
SelectionSort(num,12);//这里需要将数列元素个数传入。有心者可用sizeof在函数内求得元素个数。
for(i=0;i<12;i++)
{
printf("%d",num[i]);
}
return 0;
}
[cpp] view plain copy
//堆排序:
#include
#define MaxSize 20
typedef int KeyType; //定义关键字类型
typedef char InfoType[10];
typedef struct //记录类型
{
KeyType key; //关键字项
InfoType data; //其他数据项,类型为InfoType
} RecType; //排序的记录类型定义
//调整堆
void sift(RecType R[],int low,int high)
{
int i=low,j=2*i; //R[j]是R[i]的左孩子
RecType temp=R[i];
while (j<=high)
{
if (j=1; i--) //循环建立初始堆
sift(R,i,n);
for (i=n; i>=2; i--) //进行n-1次循环,完成推排序
{
temp=R[1]; //将第一个元素同当前区间内R[1]对换
R[1]=R[i];
R[i]=temp;
sift(R,1,i-1); //筛选R[1]结点,得到i-1个结点的堆
}
}
int main()
{
int i,n=12;
RecType R[MaxSize];
KeyType a[]= {57,40,38,11,13,34,48,75,6,19,9,7};//a[0]空闲,不作为关键字
for (i=1; i<=n; i++)
R[i].key=a[i];
printf("排序前:");
for (i=1; i<=n; i++)
printf("%d ",R[i].key);
printf("\n");
HeapSort(R,n);
printf("排序后:");
for (i=1; i<=n; i++)
printf("%d ",R[i].key);
printf("\n");
return 0;
}
[cpp] view plain copy
//归并排序:
#include
#include
#define MaxSize 20
typedef int KeyType; //定义关键字类型
typedef char InfoType[10];
typedef struct //记录类型
{
KeyType key; //关键字项
InfoType data; //其他数据项,类型为InfoType
} RecType; //排序的记录类型定义
void Merge(RecType R[],int low,int mid,int high)
{
RecType *R1;
int i=low,j=mid+1,k=0; //k是R1的下标,i、j分别为第1、2段的下标
R1=(RecType *)malloc((high-low+1)*sizeof(RecType)); //动态分配空间
while (i<=mid && j<=high) //在第1段和第2段均未扫描完时循环
if (R[i].key<=R[j].key) //将第1段中的记录放入R1中
{
R1[k]=R[i];
i++;
k++;
}
else //将第2段中的记录放入R1中
{
R1[k]=R[j];
j++;
k++;
}
while (i<=mid) //将第1段余下部分复制到R1
{
R1[k]=R[i];
i++;
k++;
}
while (j<=high) //将第2段余下部分复制到R1
{
R1[k]=R[j];
j++;
k++;
}
for (k=0,i=low; i<=high; k++,i++) //将R1复制回R中
R[i]=R1[k];
}
void MergePass(RecType R[],int length,int n) //对整个数序进行一趟归并
{
int i;
for (i=0; i+2*length-1
#include
#include
#define MAXE 20 //线性表中最多元素个数
#define MAXR 10 //基数的最大取值
#define MAXD 8 //关键字位数的最大取值
typedef struct node
{
char data[MAXD]; //记录的关键字定义的字符串
struct node *next;
} RecType;
void CreaLink(RecType *&p,char *a[],int n);
void DispLink(RecType *p);
void RadixSort(RecType *&p,int r,int d) //实现基数排序:*p为待排序序列链表指针,r为基数,d为关键字位数
{
RecType *head[MAXR],*tail[MAXR],*t; //定义各链队的首尾指针
int i,j,k;
for (i=0; i<=d-1; i++) //从低位到高位循环
{
for (j=0; jdata[i]-'0'; //找第k个链队
if (head[k]==NULL) //进行分配
{
head[k]=p;
tail[k]=p;
}
else
{
tail[k]->next=p;
tail[k]=p;
}
p=p->next; //取下一个待排序的元素
}
p=NULL; //重新用p来收集所有结点
for (j=0; jnext=head[j];
t=tail[j];
}
}
t->next=NULL; //最后一个结点的next域置NULL
//以下的显示并非必要
printf(" 按%d位排序\t",i);
DispLink(p);
}
}
void CreateLink(RecType *&p,char a[MAXE][MAXD],int n) //采用后插法产生链表
{
int i;
RecType *s,*t;
for (i=0; idata,a[i]);
if (i==0)
{
p=s;
t=s;
}
else
{
t->next=s;
t=s;
}
}
t->next=NULL;
}
void DispLink(RecType *p) //输出链表
{
while (p!=NULL)
{
printf("%c%c ",p->data[1],p->data[0]);
p=p->next;
}
printf("\n");
}
int main()
{
int n=12,d=2;
int i,j,k;
RecType *p;
char a[MAXE][MAXD];
int b[]= {57,40,38,11,13,34,48,75,6,19,9,7};
for (i=0; i
运行结果:
学习心得:
通过这次的训练,我学会了许多排序算法。