2022.09.21work

快速排序

void quick_sort(int *arr,int begin,int end)
{
	if(begin>end){
		return;
	}
	int i = begin;
	int j = end;
	int x = arr[begin];
	while(i<j){
		while(arr[j]>=x && j>i){
			j--;
		}
		arr[i] = arr[j];
		while(arr[i]<=x && j>i){
			i++;
		}
		arr[j]=arr[i];	
	}
	arr[i]=x;
	quick_sort(arr,begin,i-1);
	quick_sort(arr,i+1,end);
}

main.c

#include <stdio.h>
#include "qs.h"

int main(int argc, const char *argv[])
{
	int arr[]={54,5,8,24,6,45,23,63,2,16,42,25,15,85};
	int n = sizeof(arr)/sizeof(arr[0]);
	quick_sort(arr,0,n-1);
	for(int i=0;i<n;i++){
		printf("%d ",arr[i]);
	}
	return 0;
}

哈希查找

hash.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash.h"

// 创建hash表
nop *hash_create(int *arr,int n)
{
	nop *hash = (nop *)malloc(sizeof(nop)*n);
    if(NULL==hash){
        printf("创建失败\n");
        return NULL;
    }
	memset(hash,0,sizeof(nop)*n);
	for(int i=0;i<n;i++){
		ele_add(hash,arr[i],n);
	}
	return hash;
}


// 添加元素
void ele_add(nop *hash,datatype e,int n)
{
	int pos = e%n;
	nop l = (nop)malloc(sizeof(node));
	if(NULL==l){
		printf("添加元素失败\n");
		return ;
	}
	l->data = e;
	l->next = hash[pos];
	hash[pos] = l;
}

// 显示hash表
void hash_display(nop *hash,int n)
{
	for(int i=0;i<n;i++){
		printf(":");
		nop p = hash[i];
		while(p){
			printf("%d,",p->data);
			p = p->next;
		}
		putchar(10);
	}	
}

hash.h

#ifndef __HASH_H_
#define __HASH_H_

typedef int datatype;

typedef struct Node
{
	datatype data;
	struct Node *next;
}node,*nop;

// 创建hash表 
nop *hash_create(int *arr,int n);


// 添加元素
void ele_add(nop *hash,datatype e,int n);

// 显示hash表
void hash_display(nop *hash,int n);

#endif

main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash.h"

int main(int argc, const char *argv[])
{
	int arr[]={54,6,4,68,4,84,37,5,31,64,46,5,35,1,68,15,61};
	int n = sizeof(arr)/sizeof(arr[0]);
	
	nop *hash = hash_create(arr,n);
	hash_display(hash,n);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值