当前位置:网站首页>作业7.25 排序与查找
作业7.25 排序与查找
2022-07-26 14:00:00 【不知名大学生M】
1. 使用哈希存储将数据存入哈希表中,并进行查找
1、已知一组关键字序列为(25,51,8,22,26,67,11,16,54,41)
2、其哈希地址空间为[0…12]
3、Hash函数定义为:H(key) = key MOD 13
4、使用链地址法处理冲突,画出对应的哈希表,并编码实现
函数代码
初始化哈希表函数
//初始化哈希表
void init_hash(Node *hash[])
{
for(int i=0;i<P;i++)
{
hash[i]=NULL;
}
printf("初始化成功\n");
}
将元素存入哈希表函数
//将元素存入哈希表
int insert_hash(Node *hash[],int x)
{
int index=x%P; //定位要存储的链表
//将数据封装成结点
Node *q=(Node*)malloc(sizeof(Node));
if(NULL==q)
{
printf("空间申请失败\n");
return -1;
}
q->data=x;
q->next=NULL;
//使用头插法将结点放入链表
q->next=hash[index];
hash[index]=q;
return 0;
}
查看哈希表函数
//查看哈希表函数
void show_hash(Node **hash)
{
for(int i=0;i<P;i++)
{
printf("%d:",i);
Node *q=hash[i]; //定义遍历指针
while(q!=NULL)
{
printf("%d->",q->data);
q=q->next;
}
printf("NULL\n");
}
}
哈希查找函数
//定义哈希查找函数
void search_hash(Node **hash,int key)
{
int index=key%P; //定位要查找的值所在链表
//定义遍历指针遍历链表
Node *q=hash[index];
while(q!=NULL&&q->data!=key)
{
q=q->next;
}
//对找到的结点判断
if(NULL==q)
{
printf("查找失败\n");
}
else
{
printf("您要查找的数据%d在表中\n",key);
}
}
主函数代码
#include "hash.h"
int main(int argc, const char *argv[])
{
int arr[N]={
25,51,8,22,26,67,11,16,54,41};
Node *hash[P]; //哈希表
init_hash(hash); //初始化哈希表
for(int i=0;i<N;i++) //存入元素
{
insert_hash(hash,arr[i]);
}
//调用查看函数
show_hash(hash);
//调用查找函数
search_hash(hash,41);
return 0;
}
运行结果
初始化哈希表,再将关键字序列存入哈希表,完成哈希表的构建
打印出哈希表
查找关键字41是否在哈希表中
查找到,显示查找的数据41在表中

2. 使用冒泡排序、选择排序、插入排序、快速排序完成下面案例
案例效果图
案例要求
2)使用冒泡排序、选择排序、插入排序、快速排序对序列{198,289,98,357,85,170,232,110}从小到大排序
3)排序后根据效果图正确输出
4)功能代码加入注释
5)分析时间复杂度
函数代码
冒泡排序
//冒泡排序
void pop_sort(int *arr,int n)
{
for(int i=0;i<n-1;i++) //外层循环控制趟数
{
int flag=0; //排序开始前设立一个旗帜
for(int j=0;j<n-i-1;j++) //控制元素下标,以及比较次数
{
if(arr[j]>arr[j+1]) //大升小降
{
int temp=arr[j]; //三杯水
arr[j]=arr[j+1];
arr[j+1]=temp;
flag=1;
}
}
if(flag=0) 说明当前趟没有数据进行交换,也反应了数列已经有序,后面就不需要再进行排了
{
break;
}
}
}
选择排序
//选择排序
void select_sort(int *arr,int n)
{
for(int i=0;i<n;i++) //将所有的元素遍历一遍
{
int index=i; //将待排序的第一个元素的下标当做最小值下标
for(int j=i+1;j<n;j++) //从待排序的序列中找最小值
{
if(arr[index]>arr[j])
{
index=j; 更新最小值下标
}
}
//判断最小值下标是不是待排序序列的第一个元素
if(index!=i)
{
//交换三部曲
int temp=arr[index];
arr[index]=arr[i];
arr[i]=temp;
}
}
}
插入排序
void insert_sort(int *arr,int n)
{
for(int i=1;i<n;i++) //从前往后 遍历待排序序列
{
int temp=arr[i];
int j;
for(j=i;j>0&&temp<arr[j-1];j--) // 从后往前 遍历已排序序列
{
arr[j]=arr[j-1];
}
arr[j]=temp;
}
}
快速排序
int part(int *arr,int low,int high)
{
int x=arr[low]; //选定基准
while(low<high) //控制循环不至于一遍过后就结束
{
while(arr[high]>=x&&low<high) //防止low超过high
{
high--;
}
arr[low]=arr[high];
while(arr[low]<=x&&low<high) //防止low超过high
{
low++;
}
arr[high]=arr[low];
}
arr[low]=x; //将基准放入中间位置
return low; //将基准所在的位置返回
}
void quick_sort(int *arr,int low,int high)
{
if(low<high)
{
int mid=part(arr,low,high); //调用一趟快速排序
quick_sort(arr,low,mid-1); //对基准左侧的序列进行快速排序
quick_sort(arr,mid+1,high); //对基准右侧的序列进行快速排序
}
}
主函数代码
#include "sort.h"
int main(int argc, const char *argv[])
{
int arr[8]={
198,289,98,357,85,170,232,110};
printf("排序前:\n");
output(arr,8);
printf("排序后:\n");
pop_sort(arr,8);
printf("冒泡排序:");
output(arr,8);
printf("选择排序:");
select_sort(arr,8);
output(arr,8);
printf("插入排序:");
insert_sort(arr,8);
output(arr,8);
printf("快速排序:");
quick_sort(arr,0,7);
output(arr,8);
//int a=half_search(arr,6,45);
//printf("%d\n",a);
return 0;
}
运行结果

边栏推荐
- LCL three-phase PWM rectifier (inverter)
- Synchronization mechanism of go (sync.mutex)
- LCL三相pwm整流器(逆变器)
- The.Net webapi uses groupname to group controllers to render the swagger UI
- JS download files, filesaver.js export txt and Excel files
- Docker swarm cluster builds highly available MySQL active and standby
- WPS凭什么拒绝广告?
- Plato Farm有望通过Elephant Swap,进一步向外拓展生态
- 基于机器学习的技术术语识别研究综述
- 向路由组件传递参数
猜你喜欢
![[paper reading] raw+:a two view graph propagation method with word coupling for readability assessment](/img/14/1a02d564c59724d04fce340b6b227d.png)
[paper reading] raw+:a two view graph propagation method with word coupling for readability assessment

【黑马早报】字节旗下多款APP下架;三只松鼠脱氧剂泄露致孕妇误食;CBA公司向B站索赔4.06亿;马斯克否认与谷歌创始人妻子有婚外情...

敏捷开发与DevOps的对比

Ten thousand words long article, talking about the blueprint of enterprise digital modeling

Docker integrates the redis sentinel mode (one master, two slave and three sentinels)

Pytoch learning notes (III) use, modification, training (cpu/gpu) and verification of the model

"Intermediate and advanced test questions": what is the implementation principle of mvcc?

Large and small end mode

My meeting of OA project

Construction practice of pipeline engine of engineering efficiency ci/cd
随机推荐
JS, e.pagex, pagey modal box drag
Disease knowledge discovery based on spo semantic triples
最新战报:十项认证,五项最佳实践
Construction practice of pipeline engine of engineering efficiency ci/cd
Mobile dual finger scaling event (native), e.originalevent.touches
Parent class reference to child class (parent class reference points to child class object)
@A thousand lines of work, ride the cloud together!
Red * is added to the input box to indicate mandatory items
C语言_结构体指针来访问结构体数组
MySQL sets auto increment for existing primary keys
php使用sqlserver
Segmentation fault (core dumped)
Basic syntax of MySQL DDL and DML and DQL
Focus on building four "highlands" and join hands with partners to build the national cloud!
Tdsql-c serverless: help start-ups achieve cost reduction and efficiency increase
12437 words, take you to explore the principle of RPC communication
Pytorch学习笔记(一)安装与常用函数的使用
Comparison between SIGMOD function and softmax function
搞懂MySQL的数据类型中长度含义
[oauth2] v. oauth2loginauthenticationfilter
