完善代码:# define MAX 128 main() { int i,k,m,n,result; int a[MAX]; printf("How many nodes in the array \n"); scanf("%d",&n); printf("Please input the value of array element\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("The key word?"); scanf("%d",&k); printf("Please Input Your Choice(1-2):"); scanf("%d",&m); switch(m) { case 1: printf("This is ShunXu Search!\n"); result=shx(a,n,k); if(result>=0) printf("Found No.%d elements of the array\n",result+1); else printf("Not found\n"); break; case 2: printf("This is ErFen Search!\n"); result=erfen(a,n,k); if(result>=0) printf("In the No. %d of the array\n",result+1); else printf("Not found\n"); break; default: printf("1-------------------------ShunXu Search\n"); printf("2-------------------------ErFen Search\n"); return; } } int shx (array, n, k) int array[], n, k; { int i; array[n]=k;/*watch*/ i= 0; while ( ) i++; if (i<n) return (i); else return (-1); } int erfen(int array[],int n ,int key) { int l, r,mid; l=0; r=n-1; while (l<=r) { ; if(key==array[mid]) return mid; else if(key<array[mid]) r=mid-1; else l=mid+1; } return -1; }
时间: 2024-04-27 16:23:03 浏览: 19
# define MAX 128
int shx(int array[], int n, int k); //函数声明
int erfen(int array[], int n, int key); //函数声明
int main() {
int i, k, m, n, result;
int a[MAX];
printf("How many nodes in the array:\n");
scanf("%d", &n);
printf("Please input the value of array element:\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("The key word?");
scanf("%d", &k);
printf("Please Input Your Choice(1-2):\n");
scanf("%d", &m);
switch (m) {
case 1:
printf("This is ShunXu Search!\n");
result = shx(a, n, k);
if (result >= 0)
printf("Found No.%d element(s) of the array\n", result + 1);
else
printf("Not found\n");
break;
case 2:
printf("This is ErFen Search!\n");
result = erfen(a, n, k);
if (result >= 0)
printf("In the No. %d of the array\n", result + 1);
else
printf("Not found\n");
break;
default:
printf("1-------------------------ShunXu Search\n");
printf("2-------------------------ErFen Search\n");
return 0;
}
return 0;
}
int shx(int array[], int n, int k) {
int i = 0;
array[n] = k; //将要查找的关键值放在数组最后,方便判断
while (array[i] != k)
i++;
if (i < n)
return i;
else
return -1;
}
int erfen(int array[], int n, int key) {
int l, r, mid;
l = 0;
r = n - 1;
while (l <= r) {
mid = (l + r) / 2; //每次将查找区间缩小一半
if (key == array[mid])
return mid;
else if (key < array[mid])
r = mid - 1;
else
l = mid + 1;
}
return -1;
}
这是一个完善后的 C 语言程序,它包含了两个函数:顺序查找函数 shx 和二分查找函数 erfen。程序先让用户输入一个数组,然后让用户输入一个要查找的关键值和一个选择(1 或 2,分别代表顺序查找和二分查找)。根据用户的选择,程序会调用对应的函数进行查找,并输出查找结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)