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
相关推荐
ZPC821016 小时前
docker 镜像备份
人工智能·算法·fpga开发·机器人
ZPC821016 小时前
docker 使用GUI ROS2
人工智能·算法·fpga开发·机器人
琢磨先生David16 小时前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
颜酱16 小时前
栈的经典应用:从基础到进阶,解决LeetCode高频栈类问题
javascript·后端·算法
多恩Stone17 小时前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
生信大杂烩17 小时前
癌症中的“细胞邻域“:解码肿瘤微环境的空间密码 ——Nature Cancer 综述解读
人工智能·算法
蜡笔小马17 小时前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
m0_5312371717 小时前
C语言-数组练习进阶
c语言·开发语言·算法
qq_4542450317 小时前
基于组件与行为的树状节点系统
数据结构·c#
超级大福宝17 小时前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode