用C语言编写一段哈希排序算法
时间: 2023-03-09 10:07:43 浏览: 82
哈希排序算法的C语言实现如下: #include<stdio.h> int hash[10]; // 哈希表 // 哈希函数
int hash_key(int key)
{
return key % 10;
} // 插入元素
void hash_insert(int key)
{
int addr = hash_key(key);
while (hash[addr] != 0)
addr = (addr + 1) % 10;
hash[addr] = key;
} // 哈希排序
void hash_sort()
{
int i;
for (i = 0; i < 10; i++)
{
if (hash[i] != 0)
printf("%d ", hash[i]);
}
} int main()
{
int key[] = {12, 25, 36, 20, 30, 8, 42};
int i;
for (i = 0; i < 7; i++)
{
hash_insert(key[i]);
}
hash_sort();
return 0;
}
相关问题
用C语言编写哈希排序算法
哈希排序(Hash Sort)是一种利用哈希函数将记录映射到数组下标以实现排序的算法。下面是用C语言编写的哈希排序算法实现:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 1000
#define HASH_SIZE 100
typedef struct Node {
int data;
struct Node *next;
} Node;
void insert(Node **hash_table, int data) {
int index = data % HASH_SIZE;
Node *p = (Node *) malloc(sizeof(Node));
p->data = data;
p->next = hash_table[index];
hash_table[index] = p;
}
void hash_sort(int *arr, int n) {
Node *hash_table[HASH_SIZE] = {0};
for (int i = 0; i < n; i++)
insert(hash_table, arr[i]);
int k = 0;
for (int i = 0; i < HASH_SIZE; i++) {
Node *p = hash_table[i];
while (p) {
arr[k++] = p->data;
p = p->next;
}
}
}
int main() {
int n, arr[MAX_SIZE];
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements: ");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
hash_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
```
首先定义了一个结构体`Node`,表示哈希表中的链表结点。每个结点包含一个数据成员和一个指向下一个结点的指针。同时定义了常量`MAX_SIZE`和`HASH_SIZE`,分别表示数组最大长度和哈希表大小。
定义了一个`insert`函数,用于将指定的数据插入到哈希表中。计算数据的哈希值并取模得到数组下标,然后将新结点插入到链表的头部。
定义了一个`hash_sort`函数,用于实现哈希排序。先创建一个哈希表数组`hash_table`,将所有元素插入到哈希表中。然后按照哈希表的顺序遍历哈希表,将链表中的元素依次存入原数组中。
最后在主函数中读入数组元素,调用`hash_sort`函数对其排序,并输出结果。
用c语言写一个哈希桶算法
### 回答1:
哈希桶算法是一种常用的数据结构,它通过将数据的键映射到一个哈希表中的桶来存储数据。哈希桶算法可以提供快速的插入、删除和查找操作,具有较好的平均时间复杂度。
在 C 语言中实现哈希桶算法,需要首先定义一个哈希表的数据结构,其中包含一个桶数组和一些表示哈希表的元数据的字段。
例如,可以定义如下的哈希表数据结构:
```
#define TABLE_SIZE 100
typedef struct {
int key;
int value;
} Pair;
typedef struct {
Pair **buckets;
int size;
} HashTable;
```
接下来,可以定义一个哈希函数,用于将键映射到哈希表中的桶中。例如,可以使用以下的哈希函数:
```
int hash(int key) {
return key % TABLE_SIZE;
}
```
接下来,可以实现哈希表的插入、删除和查找操作。例如,可以使用以下的函数来插入一个键值对:
```
void insert(HashTable *table, int key, int value) {
int index = hash(key);
Pair *pair = (Pair*) malloc(sizeof(Pair));
pair->key = key;
pair->value = value;
table->buckets[index] = pair;
table->size++;
}
```
同样地,可以使用以下的函数来删除一个键值对:
```
void remove(HashTable *table, int key) {
int
### 回答2:
哈希桶算法是一种基于哈希函数实现的数据结构,用于解决数据查找和插入的效率问题。下面是使用C语言编写哈希桶算法的步骤:
1. 定义一个哈希桶结构体,里面包含一个指向数据的指针数组和一个指向数据的计数器数组。
2. 定义哈希函数,根据输入的数据生成一个哈希值。哈希函数可以使用简单的取余操作,也可以使用更复杂的算法,如MD5或SHA。
3. 定义一个插入函数,根据生成的哈希值将数据插入到相应的桶中。如果桶中已经存在数据,则更新计数器值。
4. 定义一个查找函数,根据输入的数据生成哈希值,并在相应的桶中查找。如果桶中存在数据,则返回对应的指针。如果桶中不存在数据,则返回NULL。
下面是一个简单的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define BUCKET_SIZE 100
typedef struct {
int* data;
int* counter;
} HashBucket;
void insert(HashBucket* bucket, int value) {
int hash = value % BUCKET_SIZE;
if (bucket->counter[hash] == 0) {
bucket->data[hash] = value;
} else {
bucket->counter[hash]++;
}
}
int* find(HashBucket* bucket, int value) {
int hash = value % BUCKET_SIZE;
if (bucket->counter[hash] > 0) {
return &bucket->data[hash];
} else {
return NULL;
}
}
int main() {
HashBucket bucket;
bucket.data = (int*)malloc(sizeof(int) * BUCKET_SIZE);
bucket.counter = (int*)malloc(sizeof(int) * BUCKET_SIZE);
memset(bucket.counter, 0, sizeof(int) * BUCKET_SIZE);
insert(&bucket, 5);
insert(&bucket, 10);
insert(&bucket, 15);
int* result = find(&bucket, 10);
if (result != NULL) {
printf("Value found: %d\n", *result);
} else {
printf("Value not found.\n");
}
free(bucket.data);
free(bucket.counter);
return 0;
}
```
以上是一个简单的哈希桶算法的示例。实际应用中,可以根据具体的需求进行优化和扩展。
### 回答3:
哈希桶算法是一种常见的散列技术,用于快速定位和访问存储在哈希表中的元素。在C语言中,可以通过使用数组和链表的组合来实现哈希桶算法。
首先,创建一个结构体来表示哈希桶中的节点:
```c
struct Node {
int key;
int value;
struct Node* next;
};
```
接下来,创建一个包含指向链表头节点的指针数组的哈希表:
```c
#define SIZE 10
struct Node* hash_table[SIZE] = { NULL };
```
然后,实现哈希函数来将键映射到哈希表中的索引位置:
```c
int hash(int key) {
return key % SIZE;
}
```
接下来,实现插入元素到哈希表的函数:
```c
void insert(int key, int value) {
int index = hash(key);
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->key = key;
newNode->value = value;
newNode->next = NULL;
if (hash_table[index] == NULL) {
hash_table[index] = newNode;
} else {
struct Node* currentNode = hash_table[index];
while (currentNode->next != NULL) {
currentNode = currentNode->next;
}
currentNode->next = newNode;
}
}
```
最后,实现从哈希表中查找元素的函数:
```c
int search(int key) {
int index = hash(key);
struct Node* currentNode = hash_table[index];
while (currentNode != NULL) {
if (currentNode->key == key) {
return currentNode->value;
}
currentNode = currentNode->next;
}
return -1; // 如果未找到,则返回-1
}
```
这样,我们就实现了一个简单的哈希桶算法。你可以根据具体需求对上述函数进行修改和扩展,以满足你的实际应用场景。