用c语言1.定义长度为n的查找表,m个待查找数据,定义二叉树排序树的结点结构。 2.初始化随机数发生器,产生n个随机数构成的查找表并用简单选择排序。 3.创建顺序查找、折半查找、创建二叉排序树、二叉排序树查找,取得当前系统时间函数。 4.调用顺序查找、折半查找、二叉排序树查找函数,输出在长度为n的查找表中,查找m个数据耗费的总时间。
时间: 2023-06-26 12:07:10 浏览: 66
以下是用C语言实现的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_SIZE 100
typedef struct BSTNode {
int data;
struct BSTNode* left;
struct BSTNode* right;
} BSTNode;
void initTable(int table[], int size) {
srand(time(NULL));
for (int i = 0; i < size; i++) {
table[i] = rand() % MAX_SIZE;
}
}
void selectionSort(int table[], int size) {
int i, j, minIdx, temp;
for (i = 0; i < size - 1; i++) {
minIdx = i;
for (j = i + 1; j < size; j++) {
if (table[j] < table[minIdx]) {
minIdx = j;
}
}
if (minIdx != i) {
temp = table[i];
table[i] = table[minIdx];
table[minIdx] = temp;
}
}
}
int sequentialSearch(int table[], int size, int key) {
for (int i = 0; i < size; i++) {
if (table[i] == key) {
return i;
}
}
return -1;
}
int binarySearch(int table[], int size, int key) {
int low = 0, high = size - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (table[mid] == key) {
return mid;
} else if (table[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
BSTNode* createBST(int table[], int size) {
BSTNode* root = NULL;
for (int i = 0; i < size; i++) {
BSTNode* newNode = (BSTNode*)malloc(sizeof(BSTNode));
newNode->data = table[i];
newNode->left = newNode->right = NULL;
if (root == NULL) {
root = newNode;
} else {
BSTNode* current = root;
while (1) {
if (table[i] < current->data) {
if (current->left == NULL) {
current->left = newNode;
break;
}
current = current->left;
} else {
if (current->right == NULL) {
current->right = newNode;
break;
}
current = current->right;
}
}
}
}
return root;
}
int BSTSearch(BSTNode* root, int key) {
while (root != NULL) {
if (root->data == key) {
return 1;
} else if (root->data > key) {
root = root->left;
} else {
root = root->right;
}
}
return 0;
}
int main() {
int table[MAX_SIZE], m;
printf("请输入查找表的长度(不超过100):");
int n;
scanf("%d", &n);
printf("请输入待查找的数据个数:");
scanf("%d", &m);
initTable(table, n);
selectionSort(table, n);
int key;
clock_t start, end;
double duration;
// 顺序查找
start = clock();
for (int i = 0; i < m; i++) {
key = rand() % MAX_SIZE;
sequentialSearch(table, n, key);
}
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("顺序查找 %d 个数据耗时 %lf 秒\n", m, duration);
// 折半查找
start = clock();
for (int i = 0; i < m; i++) {
key = rand() % MAX_SIZE;
binarySearch(table, n, key);
}
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("折半查找 %d 个数据耗时 %lf 秒\n", m, duration);
// 二叉排序树查找
BSTNode* root = createBST(table, n);
start = clock();
for (int i = 0; i < m; i++) {
key = rand() % MAX_SIZE;
BSTSearch(root, key);
}
end = clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("二叉排序树查找 %d 个数据耗时 %lf 秒\n", m, duration);
return 0;
}
```
这段代码实现了以下功能:
1. 初始化长度为n的随机查找表,用简单选择排序进行排序。
2. 实现顺序查找、折半查找、创建二叉排序树、二叉排序树查找。
3. 针对每种查找方法,随机生成m个数据,输出在长度为n的查找表中查找这m个数据所耗费的总时间。