各种排序和查找方式的代码实现

哈希存储

基础哈希存储(哈希冲突明显)

在这里插入图片描述

main.c

#include<stdio.h>
#include<stdlib.h>
#include"fun.h"
int main(int argc, const char *argv[])
{
    int arr[N]={25,51,8,22,26,67,11,16,54,41}; 
    datatype hash[P];
    init_hash(hash); 
//将元素存入哈希表 
    for(int i=0;i<N;i++) 
{ 
    insert_hash(hash,arr[i]); 
} 
//调用查看函数 
    show_hash(hash); 
//调用哈希查找函数 
    search_hash(hash,22);
	
	return 0;
}

fun.c

初始化哈希表

/初始化哈希表                 
void init_hash(datatype *hash)   
{                              
    for(int i=0;i<P;i++)       
    {                          
        hash[i]='\0';          
    }                          
    printf("初始化成功\n"); //不对哈希表赋初值表中没有修改的值会是随机值 
}                              
                               
        

将元素存入哈希表

//将元素存入哈希表                                
int insert_hash(datatype *hash,int x)               
{                                                 
    int index = x%P;//定位要存储的哈希表位置
	hash[index] = x;
    return 0;                                     
}     

定义查看哈希表函数

void show_hash(datatype *hash)                      
{                                                 
    for(int i=0;i<P;i++)                          
    {                                             
        printf("%d\t:",i);                          
        printf("%d\n",hash[i]);                   
    }                                             
    printf("\n");                             
}  

定义哈希查找函数

void search_hash(datatype *hash,int key)       
 {                                            
     int index = key%P;                       
     //定义遍历指针遍历链表
	 if(key==hash[index])
	 {
         printf("%d下标为%d\n",hash[index],index);                    
         printf("您要查找的数据再表中\n");    
 }                                            
 }

fun.h

#ifndef __FUN_H__
#define __FUN_H__
#define N 10
#define P 13

typedef int datatype;  
datatype hash[N];

//初始化哈希表                          
void init_hash(datatype *hash);           
                                        
//将元素存入哈希表                      
int insert_hash(datatype *hash,int x);    
                                        
                                        
                                        
//定义查看哈希表函数                    
void show_hash(datatype *hash);           
                                        
//定义哈希查找函数                      
void search_hash(datatype *hash,int key); 

#endif

链地址法处理哈希冲突

main.c

#include<stdio.h>
#include<stdlib.h>
#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,22);

	return 0;
}

hash.c

初始化哈希表

//初始化哈希表
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(q==NULL)
	{
		printf("申请失败\n");
		return -1;
	}
	q->data = x;
	q->next = NULL;
	//使用头插法将结点放入链表hash[index]
	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("fail\n");
	}else
	{
		printf("您要查找的数据再表中\n");
	}
}

hash.h

#ifndef __hash_h__
#define __hash_h__
#define N 10
#define P 13
typedef int datatype;
typedef struct Node
{
	datatype data;
	struct Node *next;
}Node;
//初始化哈希表
void init_hash(Node *hash[]);

//将元素存入哈希表
int insert_hash(Node *hash[],int x);



//定义查看哈希表函数
void show_hash(Node *hash[]);

//定义哈希查找函数
void search_hash(Node *hash[],int key);


#endif

排序方法

在这里插入图片描述

冒泡排序

#include<stdio.h>
void show(int *arr)
{
	for(int i=0;i<8;i++)
	{
		printf("%d\t",arr[i]);

	}
	printf("\n");
}
int main(int argc, const char *argv[])
{
	int temp,count=0;
	int arr[8]={198,289,98,357,85,170,232,110};
	for(int i=0;i<7;i++)//冒泡排序
	{
		count=0;
		for(int j=0;j<7-i;j++)
		{
			if(arr[j]>arr[j+1])
			{
				temp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;
				count++;//定义标识符指示是否进行交换(是否还有需要排序)
			}
		}
		if(count==0)
		{
			break;//当一趟排序结束并未发生交换,则可结束循环
		}
	}
	show(arr);
	return 0;
}

选择排序

#include<stdio.h>
void show(int *arr)
{
	for(int i=0;i<8;i++)
	{
		printf("%d\t",arr[i]);

	}
	printf("\n");
}
int main(int argc, const char *argv[])
{
	int min,count,temp=0;
	int arr[8]={198,289,98,357,85,170,232,110};
	for(int i=0;i<8;i++)
	{
		arr[min]=arr[i];
		for(int j=i+1;j<8;j++)
		{
			if(arr[j]<arr[min])
			{
				min=j;
			}
		}
		temp=arr[i];arr[i]=arr[min];arr[min]=temp;
	}
	show(arr);
	return 0;
}

插入排序

#include<stdio.h>
void show(int *arr)
{
	for(int i=0;i<8;i++)
	{
		printf("%d\t",arr[i]);

	}
	printf("\n");
}
int main(int argc, const char *argv[])
{
	int i,j;          //循环变量
	int temp;            //临时变量
	int arr[8]={198,289,98,357,85,170,232,110};


 	for(i=1; i<8; i++)        //从前往后 遍历待排序序列 
	{ 
	     int temp = arr[i];
	     for(j=i; j>0&&temp<arr[j-1]; j--)      // 从后往前 遍历已排序序列 
	     {
	         arr[j]=arr[j-1];
	    }
	     arr[j] = temp;
	}
	show(arr);
	return 0;
}

快速排序

/*********************定义一趟快排函数********************/
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);         //对基准右侧的序列进行快速排序
	}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值