快速排序
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;
}
![](https://img-blog.csdnimg.cn/e1318fa4c44f4ed68ca77f1d1017f8df.png)
哈希查找
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;
}
![](https://img-blog.csdnimg.cn/f1146ebbde944b48bc658b82b7cda389.png)