2024/3/19 数据结构day7

快速排序

#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);
	}
}

折半查找

#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;
}

哈希表查找

#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;
}
#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");

}
#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

相关推荐

  1. 数据结构day2

    2024-03-24 16:20:01       25 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-24 16:20:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-24 16:20:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-24 16:20:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-24 16:20:01       20 阅读

热门阅读

  1. Spring Boot的静态资源自动配置原理

    2024-03-24 16:20:01       22 阅读
  2. es 集群开机自动启动

    2024-03-24 16:20:01       16 阅读
  3. Rust 双向链表 LinkedList 和安全删除元素的方法

    2024-03-24 16:20:01       19 阅读
  4. cloud微服务

    2024-03-24 16:20:01       19 阅读
  5. 5.79 BCC工具之tcpaccept.py解读

    2024-03-24 16:20:01       22 阅读
  6. 外部提供控制程序

    2024-03-24 16:20:01       17 阅读
  7. Python基础语法(二)

    2024-03-24 16:20:01       21 阅读
  8. 大厂面试--列举并解释一下 http的所有请求方法?

    2024-03-24 16:20:01       17 阅读