写一个c语言的代码 要求(1) 编写顺序表(为整型数组)的顺序查找法,以及有序顺序表的二分查找的实现函数。 (2) 编写主函数main,创建具有n (n≥10)个随机元素(0-999之间)的顺序表R1,用顺序查找法查找某个值(由用户输入)出现的位置,统计查找成功(或不成功)的比较次数。将R1存储为具有n个随机元素(0-999之间)的有序顺序表(要求元素不重复,可正序或逆序),用二分查找法查找某个值(由用户输入)出现的位置,统计查找成功(或不成功)的比较次数。
时间: 2024-01-02 21:01:43 浏览: 35
下面是符合要求的 C 语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
// 顺序表数据结构
typedef struct {
int data[MAX_SIZE];
int length;
} SeqList;
// 顺序查找函数
int seqSearch(SeqList list, int key, int *cmpCount) {
int i;
for (i = 0; i < list.length; i++) {
(*cmpCount)++;
if (list.data[i] == key) {
return i;
}
}
return -1;
}
// 二分查找函数
int binarySearch(SeqList list, int key, int *cmpCount) {
int left = 0, right = list.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
(*cmpCount)++;
if (list.data[mid] == key) {
return mid;
} else if (list.data[mid] > key) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
}
int main() {
SeqList R1;
int n, i, key, pos, cmpCount = 0;
// 用户输入顺序表长度
printf("请输入顺序表长度(不少于10):");
scanf("%d", &n);
// 随机生成顺序表 R1
printf("R1 随机生成的元素为:");
for (i = 0; i < n; i++) {
R1.data[i] = rand() % 1000;
printf("%d ", R1.data[i]);
}
R1.length = n;
// 顺序查找 R1
printf("\n请输入要查找的元素:");
scanf("%d", &key);
pos = seqSearch(R1, key, &cmpCount);
if (pos == -1) {
printf("在 R1 中未找到元素 %d\n", key);
} else {
printf("在 R1 中找到元素 %d,位置为 %d\n", key, pos);
}
printf("顺序查找的比较次数为 %d\n", cmpCount);
// 排序 R1
for (i = 0; i < n; i++) {
int j, minIdx = i;
for (j = i + 1; j < n; j++) {
if (R1.data[j] < R1.data[minIdx]) {
minIdx = j;
}
}
int temp = R1.data[minIdx];
R1.data[minIdx] = R1.data[i];
R1.data[i] = temp;
}
// 去重
int k = 1;
for (i = 1; i < n; i++) {
if (R1.data[i] != R1.data[i - 1]) {
R1.data[k++] = R1.data[i];
}
}
R1.length = k;
// 二分查找 R1
printf("\nR1 排序去重后的元素为:");
for (i = 0; i < R1.length; i++) {
printf("%d ", R1.data[i]);
}
printf("\n请输入要查找的元素:");
scanf("%d", &key);
cmpCount = 0;
pos = binarySearch(R1, key, &cmpCount);
if (pos == -1) {
printf("在 R1 中未找到元素 %d\n", key);
} else {
printf("在 R1 中找到元素 %d,位置为 %d\n", key, pos);
}
printf("二分查找的比较次数为 %d\n", cmpCount);
return 0;
}
```
该程序首先让用户输入要生成的顺序表长度,并在随机生成该长度的顺序表 R1 后,使用顺序查找法查找用户输入的元素。随后,程序对 R1 进行排序和去重处理,并使用二分查找法查找用户输入的元素。最后,程序输出查找过程中比较的次数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)