用c语言帮我写一个折半查找的程序
时间: 2023-05-26 11:04:34 浏览: 51
#include <stdio.h>
int BinarySearch(int arr[], int left, int right, int key) {
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] == key) return mid;
else if (arr[mid] > key) right = mid - 1;
else left = mid + 1;
}
return -1;
}
int main() {
int arr[] = {2, 5, 7, 10, 14, 18};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 10;
int result = BinarySearch(arr, 0, n - 1, key);
if (result == -1) printf("Element not found in array\n");
else printf("Element found at index %d\n", result);
return 0;
}
相关问题
一个c语言程序完成顺序查找与折半查找代码实现
下面是一个完整的 C 语言程序,包含了顺序查找和折半查找的代码实现:
```c
#include <stdio.h>
// 顺序查找
int sequential_search(int arr[], int n, int target) {
for (int i = 0; i < n; i++) {
if (arr[i] == target) {
return i; // 返回目标元素的下标
}
}
return -1; // 如果数组中不存在目标元素,则返回-1
}
// 折半查找
int binary_search(int arr[], int n, int target) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid; // 返回目标元素的下标
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1; // 如果数组中不存在目标元素,则返回-1
}
int main() {
int arr[] = {1, 3, 5, 7, 9};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 5;
// 顺序查找
int index1 = sequential_search(arr, n, target);
if (index1 != -1) {
printf("目标元素 %d 在数组中的下标为 %d(顺序查找)\n", target, index1);
} else {
printf("数组中不存在目标元素 %d(顺序查找)\n", target);
}
// 折半查找
int index2 = binary_search(arr, n, target);
if (index2 != -1) {
printf("目标元素 %d 在数组中的下标为 %d(折半查找)\n", target, index2);
} else {
printf("数组中不存在目标元素 %d(折半查找)\n", target);
}
return 0;
}
```
这个程序首先定义了顺序查找和折半查找两个函数,然后在 `main` 函数中初始化了一个数组和目标元素,分别调用了这两个函数进行查找,并打印查找结果。
一个c语言程序完成顺序查找与折半查找输出元素位置、查找次数代码实现
以下是一个简单的C语言程序,用于实现顺序查找和折半查找,并输出元素位置和查找次数:
```c
#include <stdio.h>
int sequential_search(int arr[], int n, int x)
{
int i, count = 0;
for (i = 0; i < n; i++)
{
count++;
if (arr[i] == x)
return i;
}
printf("Total number of comparisons: %d\n", count);
return -1;
}
int binary_search(int arr[], int n, int x)
{
int low = 0, high = n - 1, mid, count = 0;
while (low <= high)
{
count++;
mid = (low + high) / 2;
if (arr[mid] == x)
{
printf("Total number of comparisons: %d\n", count);
return mid;
}
else if (arr[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
printf("Total number of comparisons: %d\n", count);
return -1;
}
int main()
{
int arr[] = { 2, 5, 7, 10, 14, 16, 19, 20, 24, 28 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 16;
int pos = sequential_search(arr, n, x);
if (pos == -1)
printf("Element not found in the array.\n");
else
printf("Element found at position %d.\n", pos);
pos = binary_search(arr, n, x);
if (pos == -1)
printf("Element not found in the array.\n");
else
printf("Element found at position %d.\n", pos);
return 0;
}
```
在这个程序中,我们首先定义了两个函数,分别是顺序查找和折半查找。在顺序查找中,我们遍历整个数组,逐个比较每个元素,直到找到目标元素。在折半查找中,我们首先将数组分成两个部分,然后比较中间元素与目标元素的大小关系,如果中间元素等于目标元素,则返回中间元素的位置;否则,如果中间元素小于目标元素,则在右半部分进行查找;否则,在左半部分进行查找。
在主函数中,我们定义了一个整型数组和目标元素x。我们首先调用顺序查找函数,并输出元素位置(如果找到)或元素未找到。然后,我们调用折半查找函数,并再次输出元素位置(如果找到)或元素未找到。在两个函数中,我们还输出了查找次数。
注意:这只是一个简单的示例程序,实际上,顺序查找和折半查找的实现方式可能会有所不同,具体取决于应用场景和数据结构。