编写一个采用折半查找法查找数组中的数的小程序。 要求输入一个数组,数组的大小为5,首先将其按照从小到大的顺序排序。 然后输入一个数,要求用折半查找法找出该数是数组中第几个元素的值。如果该数不在数组中,则输出无此数。
时间: 2023-04-30 09:05:40 浏览: 106
这是一个采用折半查找法查找数组中的数的小程序。要求输入一个数组,数组的大小为5,首先将其按照从小到大的顺序排列。然后输入一个数,要求用折半查找法找出该数是数组中第几个元素的值。如果该数不在数组中,则输出无此数。
相关问题
本关任务:编写一个采用折半查找法查找数组中的数的小程序。 要求输入一个数组,数组的大小为5,首先将其按照从小到大的顺序排序。 然后输入一个数,要求用折半查找法找出该数是数组中第几个元素的值。如果该数不在数组中,则输出无此数。
好的,我明白了您的要求。以下是一个使用折半查找法查找数组中的数的小程序。
```python
def binary_search(arr, target):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
# 输入数组,大小为5
arr = []
for i in range(5):
num = int(input("请输入第{}个数:".format(i+1)))
arr.append(num)
# 对数组进行排序
arr.sort()
# 输入要查找的数
target = int(input("请输入要查找的数:"))
# 使用折半查找法查找数在数组中的位置
index = binary_search(arr, target)
# 输出结果
if index != -1:
print("该数在数组中的位置是:", index+1)
else:
print("无此数")
```
请按照程序提示依次输入数组的五个数和要查找的数,程序将会输出该数在数组中的位置或者无此数的提示。
用C语言编写一个程序,满足任意输入10个数,先将其按由大到小的顺序排列;再输入一个数,要求用折半查找法找出该数是数组中第几个元素的值。如果该数不在数组中,则打印出无此数”。
下面是使用C语言编写的程序,满足输入10个数,先将其按由大到小的顺序排列,再输入一个数,用折半查找法找出该数是数组中第几个元素的值,如果该数不在数组中,则打印出无此数。
```c
#include <stdio.h>
void sort(int arr[], int n);
int binary_search(int arr[], int n, int target);
int main() {
int arr[10];
int target;
printf("请输入10个整数:");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
sort(arr, 10);
printf("排序后的数组为:");
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n请输入要查找的数:");
scanf("%d", &target);
int index = binary_search(arr, 10, target);
if (index == -1) {
printf("无此数\n");
} else {
printf("%d 是数组中第 %d 个元素的值\n", target, index + 1);
}
return 0;
}
void sort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int binary_search(int arr[], int n, int target) {
int left = 0;
int right = n - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] > target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
```
首先在 `main` 函数中,我们定义了一个大小为10的整型数组 `arr`,并从用户输入中读入10个整数。然后我们调用 `sort` 函数对数组进行排序,并输出排序后的结果。
接着,我们从用户输入中读入要查找的数 `target`,并调用 `binary_search` 函数进行折半查找。如果 `binary_search` 函数返回 `-1`,表示要查找的数不在数组中,我们输出“无此数”;否则,我们输出查找结果。
`sort` 函数使用的是冒泡排序算法,它可以将数组中的元素按照由大到小的顺序排列。
`binary_search` 函数使用的是折半查找算法。它首先将数组中间位置的元素与要查找的数进行比较,如果相等则返回中间位置,否则根据比较结果缩小查找范围,直到找到要查找的数或者查找范围为空为止。如果要查找的数不在数组中,则返回 `-1`。
相关推荐
![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)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)