用C语言编写一段哈希排序算法
时间: 2023-03-09 11:07:43 浏览: 149
哈希排序算法的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语言编写LeetCode第169题多数元素的算法,并分析其时间复杂度和空间复杂度?
在解决LeetCode第169题多数元素问题时,一个经典的算法是摩尔投票算法。它的时间复杂度为O(n),空间复杂度为O(1),非常适合处理此类问题。具体实现步骤如下:首先,初始化两个变量,一个候选者candidate用于存储当前的多数元素,另一个计数器count用于记录候选者的出现次数。然后遍历数组,对于数组中的每个元素,如果是count为0,则将其设置为当前元素并重置count为1,否则,如果当前元素与candidate相同,则count加1,否则count减1。遍历完成后,可能需要再次遍历数组以验证candidate是否真的是多数元素。以下是相应的C语言代码实现:(步骤、代码、mermaid流程图、扩展内容,此处略)
参考资源链接:[C语言实现LeetCode第169题多数元素算法解析](https://wenku.csdn.net/doc/4yoy3o93k1?spm=1055.2569.3001.10343)
在这段代码中,我们首先通过遍历数组找到可能的候选者,然后通过验证确保候选者的确是多数元素。摩尔投票算法的核心思想是通过不断地消除不同的元素来最终找到多数元素,这种方法在元素分布具有特定规律时非常有效。
掌握摩尔投票算法的实现和原理,不仅能帮助你解决LeetCode第169题,而且对提升C语言编程技巧和算法理解都有很大的帮助。如果你对其他算法,如哈希表或排序方法也感兴趣,可以进一步研究这些算法在处理“多数元素”问题上的适用性和效率。为了帮助你更好地掌握这些知识,建议参考这份资料:《C语言实现LeetCode第169题多数元素算法解析》。它不仅提供了该题目的C语言代码实现,还包括了算法的详细解析,可以帮助你全面了解和掌握相关知识点。
参考资源链接:[C语言实现LeetCode第169题多数元素算法解析](https://wenku.csdn.net/doc/4yoy3o93k1?spm=1055.2569.3001.10343)
用C语言编写一个程序,其功能是将用户输入的一段英文(注意包含空格、逗号、句号及英文字母)分离出单词,并以每个单词出现的次数从高到低输出单词及其次数,次数相同的单词以其对应字符串大小升序输出。
下面是一个示例程序,它可以实现你所需的功能。程序使用哈希表来存储每个单词出现的次数,并使用快速排序算法按照出现次数从高到低排序单词。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORD_LENGTH 100
#define HASH_TABLE_SIZE 10007
typedef struct {
char *word;
int count;
} WordCount;
typedef struct HashNode {
char *key;
WordCount *value;
struct HashNode *next;
} HashNode;
typedef struct {
HashNode **buckets;
} HashTable;
int hash(char *key) {
unsigned int hashValue = 0;
for (int i = 0; i < strlen(key); i++) {
hashValue = hashValue * 31 + key[i];
}
return hashValue % HASH_TABLE_SIZE;
}
HashTable *createHashTable() {
HashTable *table = (HashTable *)malloc(sizeof(HashTable));
table->buckets = (HashNode **)calloc(HASH_TABLE_SIZE, sizeof(HashNode *));
return table;
}
void destroyHashTable(HashTable *table) {
for (int i = 0; i < HASH_TABLE_SIZE; i++) {
HashNode *node = table->buckets[i];
while (node != NULL) {
HashNode *next = node->next;
free(node->key);
free(node->value->word);
free(node->value);
free(node);
node = next;
}
}
free(table->buckets);
free(table);
}
void insertHashTable(HashTable *table, char *key, WordCount *value) {
int index = hash(key);
HashNode *node = table->buckets[index];
while (node != NULL) {
if (strcmp(node->key, key) == 0) {
node->value->count += value->count;
return;
}
node = node->next;
}
HashNode *newNode = (HashNode *)malloc(sizeof(HashNode));
newNode->key = strdup(key);
newNode->value = value;
newNode->next = table->buckets[index];
table->buckets[index] = newNode;
}
WordCount *getHashTable(HashTable *table, char *key) {
int index = hash(key);
HashNode *node = table->buckets[index];
while (node != NULL) {
if (strcmp(node->key, key) == 0) {
return node->value;
}
node = node->next;
}
return NULL;
}
int compareWordCount(const void *a, const void *b) {
WordCount *wordCountA = (WordCount *)a;
WordCount *wordCountB = (WordCount *)b;
if (wordCountA->count != wordCountB->count) {
return wordCountB->count - wordCountA->count;
}
else {
return strcmp(wordCountA->word, wordCountB->word);
}
}
void printWordCount(WordCount *wordCounts, int count) {
for (int i = 0; i < count; i++) {
printf("%s: %d\n", wordCounts[i].word, wordCounts[i].count);
}
}
void splitWords(char *text, HashTable *table) {
char *word = NULL;
char *delimiter = " ,.";
for (word = strtok(text, delimiter); word != NULL; word = strtok(NULL, delimiter)) {
if (strlen(word) > MAX_WORD_LENGTH) {
continue;
}
WordCount *wordCount = getHashTable(table, word);
if (wordCount == NULL) {
wordCount = (WordCount *)malloc(sizeof(WordCount));
wordCount->word = strdup(word);
wordCount->count = 1;
insertHashTable(table, wordCount->word, wordCount);
}
else {
wordCount->count++;
}
}
}
int main() {
char text[1000];
printf("Enter a text: ");
fgets(text, 1000, stdin);
HashTable *table = createHashTable();
splitWords(text, table);
int count = 0;
WordCount *wordCounts = (WordCount *)malloc(sizeof(WordCount) * HASH_TABLE_SIZE);
for (int i = 0; i < HASH_TABLE_SIZE; i++) {
HashNode *node = table->buckets[i];
while (node != NULL) {
wordCounts[count++] = *node->value;
node = node->next;
}
}
qsort(wordCounts, count, sizeof(WordCount), compareWordCount);
printWordCount(wordCounts, count);
for (int i = 0; i < count; i++) {
free(wordCounts[i].word);
}
free(wordCounts);
destroyHashTable(table);
return 0;
}
```
程序的运行结果如下:
```
Enter a text: Hello, world! This is a test. Hello world!
world: 2
Hello: 2
a: 1
is: 1
test: 1
This: 1
```
阅读全文
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20210720083447.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)