c语言实现二分查找代码
时间: 2023-11-05 07:59:02 浏览: 66
二分查找法是一种高效的查找方法,它要求被查找的数组必须有序。以下是C语言实现二分查找法的代码:
```c
void BinarySearch(int* arr, int key, int left, int right) {
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] > key) {
right = mid - 1;
} else if (arr[mid] < key) {
left = mid + 1;
} else {
printf("找到了,对应的下标为:%d\n", mid);
return;
}
}
printf("找不到\n");
}
```
在这个代码中,我们通过不断缩小查找范围,从数组的中间开始与目标数字比较。如果中间元素大于目标数字,那么目标数字一定在左侧范围内,将右指针移到中间元素的左侧;如果中间元素小于目标数字,那么目标数字一定在右侧范围内,将左指针移到中间元素的右侧;如果中间元素等于目标数字,则找到了目标数字的下标。如果左指针大于右指针,表示没有找到目标数字。
相关问题
用c语言实现二分查找
二分查找是一种高效的查找算法,可以在有序数组中快速查找指定元素。以下是用C语言实现二分查找的代码示例:
```
int binarySearch(int arr[], int left, int right, int target) {
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;
}
```
该函数接受一个有序数组arr、数组左边界left、数组右边界right和目标元素target作为参数,返回目标元素在数组中的下标,如果目标元素不存在于数组中,则返回-1。
函数的实现思路是:首先计算出数组的中间位置mid,然后将目标元素与中间位置的元素进行比较,如果相等则返回mid,如果目标元素比中间位置的元素大,则在右半部分继续查找,否则在左半部分继续查找,直到找到目标元素或者左右边界重合为止。
c语言的二分查找代码
### 回答1:
答:下面是实现二分查找的c语言代码:// C program to implement binary search
#include <stdio.h>
// A recursive binary search function. It returns
// location of x in given array arr[l..r] is present,
// otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l)/2;
// If the element is present at the middle
// itself
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid-1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid+1, r, x);
}
// We reach here when element is not
// present in array
return -1;
}
int main(void)
{
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr)/ sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n-1, x);
(result == -1)? printf("元素不在数组中")
: printf("元素在数组的索引为 %d", result);
return 0;
}
### 回答2:
二分查找是一种高效的查找算法,其适用于已排序的数组。以下是C语言实现的二分查找代码:
```c
#include <stdio.h>
// 二分查找函数
int binarySearch(int arr[], int start, int end, int target) {
while (start <= end) {
int mid = start + (end - start) / 2;
// 如果目标值等于中间元素,则返回对应索引
if (arr[mid] == target) {
return mid;
}
// 如果目标值小于中间元素,则在左侧继续查找
if (arr[mid] > target) {
end = mid - 1;
}
// 如果目标值大于中间元素,则在右侧继续查找
else {
start = mid + 1;
}
}
// 如果未找到目标值,则返回-1
return -1;
}
int main() {
int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 23;
int result = binarySearch(arr, 0, n - 1, target);
// 输出结果
if (result == -1) {
printf("目标值 %d 未找到。\n", target);
} else {
printf("目标值 %d 所在的索引为 %d。\n", target, result);
}
return 0;
}
```
以上代码实现了一个名为`binarySearch`的二分查找函数。在`main`函数中,我们声明了一个已排序的整数数组`arr`,并使用`binarySearch`函数在其中查找目标值`target`。最后,根据查找结果输出对应的信息。
执行上述代码,输出为"目标值23所在的索引为5",即目标值23在数组arr的索引5处。如果目标值不在数组中,则输出"目标值23未找到"。
### 回答3:
二分查找(Binary Search)是一种在有序数组中查找目标值的算法。以下是使用C语言实现二分查找的代码:
```c
#include <stdio.h>
int binarySearch(int arr[], int target, int low, int high) {
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) {
return mid; // 目标值在数组中的索引
} else if (arr[mid] < target) {
low = mid + 1; // 目标值在右侧区域
} else {
high = mid - 1; // 目标值在左侧区域
}
}
return -1; // 目标值不存在于数组中
}
int main() {
int arr[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; // 有序数组
int target = 12; // 目标值
int arrSize = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, target, 0, arrSize - 1);
if (result == -1) {
printf("目标值不存在于数组中\n");
} else {
printf("目标值在数组中的索引为:%d\n", result);
}
return 0;
}
```
以上代码实现了二分查找算法。首先,通过定义一个函数`binarySearch`来执行二分查找操作,该函数接受一个有序数组、目标值、待查找区间的起始索引和终止索引作为参数。在函数中使用`while`循环进行查找操作,如果目标值与中间元素相等,则返回中间元素的索引,如果目标值比中间元素大,则在右侧区域查找,将`low`更新为`mid + 1`,如果目标值比中间元素小,则在左侧区域查找,将`high`更新为`mid - 1`。如果最终未找到目标值,则返回-1表示目标值不存在于数组中。
在`main`函数中,定义了一个有序数组和目标值,并将数组的大小计算出来。然后调用`binarySearch`函数进行二分查找,并将结果存储在`result`变量中。最后根据`result`的值输出结果。
以上就是使用C语言实现二分查找的代码。