一、折半查找算法的C语言实现:

#include <stdio.h>

int binarySearch(int arr[], int low, int high, int target) {
    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (arr[mid] == target) {
            return mid;
        } else if (arr[mid] < target) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    return -1;
}

int main() {
    int arr[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
    int n = sizeof(arr) / sizeof(arr[0]);
    int target = 9;
    int index = binarySearch(arr, 0, n - 1, target);
    if (index != -1) {
        printf("Target found at index %d\n", index);
    } else {
        printf("Target not found\n");
    }
    return 0;
}

二、二叉排序树的查找算法的C语言实现:

#include <stdio.h>
#include <stdlib.h>

struct TreeNode {
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
};

struct TreeNode* createNode(int val) {
    struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    newNode->val = val;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}

struct TreeNode* insert(struct TreeNode* root, int val) {
    if (root == NULL) {
        return createNode(val);
    }
    if (val < root->val) {
        root->left = insert(root->left, val);
    } else if (val > root->val) {
        root->right = insert(root->right, val);
    }
    return root;
}

struct TreeNode* search(struct TreeNode* root, int val) {
    if (root == NULL || root->val == val) {
        return root;
    }
    if (val < root->val) {
        return search(root->left, val);
    } else {
        return search(root->right, val);
    }
}

int main() {
    int arr[] = {5, 3, 7, 2, 4, 6, 8};
    int n = sizeof(arr) / sizeof(arr[0]);
    struct TreeNode* root = NULL;
    for (int i = 0; i < n; i++) {
        root = insert(root, arr[i]);
    }
    int target = 6;
    struct TreeNode* result = search(root, target);
    if (result != NULL) {
        printf("Target found\n");
    } else {
        printf("Target not found\n");
    }
    return 0;
}

三、哈希表查找的C语言实现:

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

#define TABLE_SIZE 10

struct Node {
    char name[20];
    struct Node* next;
};

struct HashTable {
    struct Node* table[TABLE_SIZE];
};

int hash(char* name) {
    int sum = 0;
    for (int i = 0; i < strlen(name); i++) {
        sum += name[i];
    }
    return sum % TABLE_SIZE;
}

void insert(struct HashTable* ht, char* name) {
    int index = hash(name);
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    strcpy(newNode->name, name);
    newNode->next = NULL;
    if (ht->table[index] == NULL) {
        ht->table[index] = newNode;
    } else {
        struct Node* curr = ht->table[index];
        while (curr->next != NULL) {
            curr = curr->next;
        }
        curr->next = newNode;
    }
}

int search(struct HashTable* ht, char* name) {
    int index = hash(name);
    struct Node* curr = ht->table[index];
    while (curr != NULL) {
        if (strcmp(curr->name, name) == 0) {
            return 1;
        }
        curr = curr->next;
    }
    return 0;
}

int main() {
    struct HashTable ht;
    for (int i = 0; i < TABLE_SIZE; i++) {
        ht.table[i] = NULL;
    }
    insert(&ht, "Alice");
    insert(&ht, "Bob");
    insert(&ht, "Charlie");
    char* target = "Bob";
    if (search(&ht, target)) {
        printf("Target found\n");
    } else {
        printf("Target not found\n");
    }
    return 0;
}

以上代码分别实现了有序表的折半查找算法、二叉排序树的查找算法和哈希表查找算法。你可以根据需要调整具体的数据和查找目标进行调试

用C语言编写可运行的代码实现以下功能一、用C语言编程实现有序表的折半查找算法1、	创建一个递增的有序表;2、	给定一个值用折半查找算法在有序表中进行查找;3、	输出查找结果;4、	给定具体数据调试程序二、用C语言编程实现在二叉排序树中进行查找1、读入一串整数采用二叉链存储结构创建一棵二叉排序树;2、给定一个值在二叉排序树中进行查找;3、输出查找结果;4、给定具体数据调试程序。三、哈希表查找针对某个

原文地址: https://www.cveoy.top/t/topic/hzQd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录