2024/3/19 数据结构day7

快速排序

cs 复制代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void sort(int *arr,int low,int high);
int one_sort(int *arr,int low,int high);
int main(int argc, const char *argv[])
{
	int arr[]={1,5,90,78,66,100,45,33};
	int len=sizeof(arr)/sizeof(arr[0]);
	sort(arr,0,len-1);

	for(int i=0;i<len;i++)
	{
		printf("%d ",arr[i]);
	    }

	return 0;
}
int one_sort(int *arr,int low,int high)
{
	int base=arr[low];
	while(high>low)
	{
		while(high>low&&arr[high]>base)
		{
			high--;
		}
		arr[low]=arr[high];
		while(high>low&&arr[low]<base)
		{
			low++;
		}
		arr[high]=arr[low];
	}
	arr[low]=base;
	return low;
}

void sort(int *arr,int low,int high)
{
	if(high>low)
	{
		int ret=one_sort(arr,low,high);
		sort(arr,low,ret-1);
		sort(arr,ret+1,high);
	}
}

折半查找

cs 复制代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int half_search(int *arr,int low,int high,int k);
int main(int argc, const char *argv[])
{
	int arr[]={12,34,54,67,88,99,100};
	int len=sizeof(arr)/sizeof(arr[0]);
	int k=88;
	int num=half_search(arr,0,len-1,k);
	printf("找到元素下标为%d\n",num);
	return 0;
}
int half_search(int *arr,int low,int high,int k)
{
	while(low<=high)
	{
		int mid=(low+high)/2;
		if(arr[mid]==k)
		{
			return mid;
		}
		else if(arr[mid]>k){
			high=mid-1;
		}
		else if(arr[mid]<k){
			low=mid+1;
		}
	}
	return -1;
}

哈希表查找

cs 复制代码
#include "hash.h"
int main()
{
	int arr[]={25,51,8,22,26,67,11,16,54,41};
	int len=sizeof(arr)/sizeof(arr[0]);
	int hash_len=len*4/3;
	node_p H[MAX]={0};
	for(int i=0;i<len;i++)
	{
		insert_hash(H,arr[i]);
	}
	show(H);
	find(H,25);
	return 0;
}
cs 复制代码
#include "hash.h"
//申请结点
node_p create_node(int data)
{
	node_p new=(node_p)malloc(sizeof(node));
	if(new==NULL)
	{
		printf("申请空间失败");
		return NULL;
    }
	new->data=data;
	return new;
}
//存入哈希表
void insert_hash(node_p H[],int key)
{
	int i=key%MAX;
	node_p new=create_node(key);
	new->next=H[i];
	H[i]=new;
}
//输出哈希表
void show(node_p *H)
{
	if(H==NULL)
	{
		printf("入参为空");
		return ;
	}
	for(int i=0;i<MAX;i++)
	{
		node_p p=H[i];
		while(p!=NULL)
		{
			printf("%d->",p->data);
			p=p->next;
		}
		printf("NULL\n");
	}
}
//查找元素
void find(node_p *H,int key)
{
	int num=key%MAX;
	int count=1;
	node_p p=H[num];
	while(p!=NULL)
	{
		if(p->data==key)
		{
			printf("该元素在数组中的下标为%d\n",num);
			printf("该元素在链表中的位置为%d\n",count);
			return ;
		}
		p=p->next;
		count++;
	}
	printf("NO find");

}
cs 复制代码
#ifndef __HASH_H__
#define __HASH_H__
#include <stdio.h>
#include <stdlib.h>
#define MAX 13
typedef struct node
{
	int data;
	struct node *next;
}node,*node_p;


//申请结点
node_p create_node(int data);
//存入哈希表
void insert_hash(node_p H[],int key);
//输出哈希表
void show(node_p *H);
//查找元素
void find(node_p *H,int key);


#endif
相关推荐
fangzt20105 小时前
从零搭建自动驾驶中间件(四):数据录制与回灌——算法调试的核心基础设施
算法·中间件·自动驾驶
人道领域5 小时前
【LeetCode刷题日记】二叉树层序遍历完全指南:从基础到LeetCode实战一篇搞定BFS模板,秒杀4道经典面试题
java·开发语言·数据结构·leetcode·面试·二叉树
搬砖的小码农_Sky5 小时前
比特币区块链:SHA256哈希函数
算法·区块链·哈希算法
承渊政道5 小时前
【动态规划算法】(一文讲透二维费用的背包问题)
数据结构·c++·学习·算法·leetcode·动态规划·哈希算法
Zevalin爱灰灰11 小时前
现代密码学 第二章——流密码【下】
算法·密码学
飞Link13 小时前
大模型长文本的“救命稻草”:深度解析 TurboQuant 与 KV Cache 压缩技术
算法
郝学胜-神的一滴14 小时前
深度学习优化核心:梯度下降与网络训练全解析
数据结构·人工智能·python·深度学习·算法·机器学习
Je1lyfish14 小时前
CMU15-445 (2025 Fall/2026 Spring) Project#3 - QueryExecution
linux·c语言·开发语言·数据结构·数据库·c++·算法
许彰午14 小时前
03-二叉树——从递归遍历到非递归实现
java·算法
Brilliantwxx15 小时前
【C++】 vector(代码实现+坑点讲解)
开发语言·c++·笔记·算法