【第十六周 项目1(6)-基数排序】
来源:互联网 发布:知乎怎么设置隐私 编辑:程序博客网 时间:2024/05/28 18:44
问题及代码:
/*
* Copyright (c)2015级,烟台大学 计算机与控制工程学院
* All rights reserved.
* 文件名称:项目1.cpp
* 作 者:胡馨月
* 完成日期:2016年12月15日
* 版 本 号:v1.0
*问题描述:验证基数排序,完成测试。
*输入描述:无
*程序输出:测试数据
*/
基数排序:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#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; j<r; j++) //初始化各链队首、尾指针
head[j]=tail[j]=NULL;
while (p!=NULL) //对于原链表中每个结点循环
{
k=p->data[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; j<r; j++) //对于每一个链队循环
if (head[j]!=NULL) //进行收集
{
if (p==NULL)
{
p=head[j];
t=tail[j];
}
else
{
t->next=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; i<n; i++)
{
s=(RecType *)malloc(sizeof(RecType));
strcpy(s->data,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=10,r=10,d=2;
int i,j,k;
RecType *p;
char a[MAXE][MAXD];
int b[]= {75,23,98,44,57,12,29,64,38,82};
for (i=0; i<n; i++) //将b[i]转换成字符串
{
k=b[i];
for (j=0; j<d; j++) //例如b[0]=75,转换后a[0][0]='7',a[0][1]='5'
{
a[i][j]=k%10+'0';
k=k/10;
}
a[i][j]='\0';
}
CreateLink(p,a,n);
printf("\n");
printf(" 初始关键字\t"); //输出初始关键字序列
DispLink(p);
RadixSort(p,10,2);
printf(" 最终结果\t"); //输出最终结果
DispLink(p);
printf("\n");
return 0;
}
运行截图:
- 第十六周项目1-(6)基数排序
- 第十六周项目1-(6)基数排序
- 第十六周项目1(6) 基数排序
- 【第十六周 项目1(6)-基数排序】
- 第十六周 项目1-(6)基数排序
- 第十六周项目1(8)-基数排序
- 第十六周项目1(6)——基数排序
- 第十六周项目1(6)——基数排序
- 第十六周项目1(6)--验证算法之基数排序
- 第十六周项目1(6)——基数排序
- 第十六周项目1-基数排序
- 第十六周项目1基数排序
- 第十六周项目-基数排序
- 第十六周项目一验证算法(6)基数排序
- (第十六周项目1)验证算法(8)基数排序
- 第十六周项目1-验证算法(8)基数排序
- 第十六周项目1--验证算法--(8)基数排序
- 第十六周 项目1--验证算法(8)--基数排序
- 收集mysql负载shell脚本
- solr 5.5以上版本找不到Schema.xml
- 第14周项目2 二叉排序树中查找的路径
- 第十六周 项目1验证算法-快速排序
- Xcode8 code=3000“未找到应用程序的aps-environment”的授权字符串"
- 【第十六周 项目1(6)-基数排序】
- GridLayoutManager 某些item 独站一行
- PHP中VC6、VC9、TS、NTS版本的区别与用法详解
- Win7x64+cuda8.0+ Microsoft caffe VS2013配置记录
- 【第16周 项目1(3) - 直接选择排序】
- Spring核心思想,IoC与DI详解(如果还不明白,放弃java吧)
- 开发调试MVC过程中,出现csc.exe停止工作,错误代码为 255
- 第十六周项目1(4)--验证算法之堆排序
- Centos 6.5 Install Mysql 8.0.0