简单的二分查找

个人认为比较简单的二分查找

bool seek(int l,int r,int cmp)
{
    while(l<=r)
    {
        m=l+(r-l)/2;//m为中点值
        //二分搜索
        if(a[m]>cmp)r=m-1;
        if(a[m]<cmp)l=m+1;
        if(a[m]==cmp)return 1;//找到了返回1
    }
    return 0;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 二分查找是一种在有序数组中查找特定元素的搜索算法。它的基本思想是将数组分成两个部分,每次比较中间元素的值和目标值的大小,并根据结果将搜索区间缩小一半。 下面是用 Java 实现二分查找的代码示例: ``` public class BinarySearch { public static int binarySearch(int[] array, int target) { int left = 0; int right = array.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (array[mid] == target) { return mid; } else if (array[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; // target not found } public static void main(String[] args) { int[] array = {1, 3, 5, 7, 9}; int target = 3; int result = binarySearch(array, target); if (result != -1) { System.out.println("Target found at index: " + result); } else { System.out.println("Target not found"); } } } ``` 在这段代码中,我们定义了一个名为 `binarySearch` 的函数,它接受两个参数:一个数组和一个目标值。函数通过使用二分查找算法在数组中查找目标值,并返回目标值在数组中的位置(如果找到的话)。否则,函数返回 -1。 在 `main` 函数中,我们测试了这个函数,并在数组 `{1, 3, 5, 7, 9}` 中查找目标值 3。如果找到了,就会输出 "Target found at index: 1"(因为 3 在数组中 ### 回答2: 二分查找(Binary Search)是一种常用的查找算法,它适用于有序的数组。通过将待查找的元素与数组中间元素进行比较,判断待查找元素在数组的左边还是右边,从而缩小查找范围,直到找到目标元素或范围为空为止。 以下是使用Java语言编写二分查找的示例代码: ```java public class BinarySearch { // 二分查找方法 public static int binarySearch(int[] array, int target) { int left = 0; int right = array.length - 1; while (left <= right) { int mid = (left + right) / 2; if (array[mid] == target) { return mid; // 找到目标元素,返回索引 } if (array[mid] < target) { left = mid + 1; // 目标元素在右半部分,更新左边界 } else { right = mid - 1; // 目标元素在左半部分,更新右边界 } } return -1; // 没有找到目标元素,返回-1 } public static void main(String[] args) { int[] array = {1, 3, 5, 7, 9, 11, 13}; int target = 7; int index = binarySearch(array, target); if (index != -1) { System.out.println("目标元素" + target + "的索引是:" + index); } else { System.out.println("没有找到目标元素" + target); } } } ``` 以上代码实现了一个简单二分查找算法,首先定义了一个`binarySearch`方法用于查找目标元素在数组中的索引,然后在`main`方法中定义一个有序数组和目标元素,并调用`binarySearch`方法进行查找。如果找到目标元素,则输出目标元素的索引;如果未找到目标元素,则输出未找到的提示信息。 在上面的示例中,通过不断更新查找范围的左右边界,最终找到了目标元素7,并输出它在数组中的索引为3。如果目标元素不在数组中,则输出未找到的提示信息。 这就是使用Java编写的简单二分查找算法的实现。 ### 回答3: 二分查找也称为折半查找,是一种高效的查找算法。在查找有序数组中的元素时,通过将数组分成两部分,将目标值与数组中间元素进行比较,可以快速确定目标值所在的范围,并且每一次比较都可以将查找范围减半,从而提高查找效率。 以下是用Java语言实现二分查找的示例代码: ```java public class BinarySearch { public static int binarySearch(int[] array, int target) { int left = 0; int right = array.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (array[mid] == target) { return mid; } else if (array[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; } public static void main(String[] args) { int[] array = {1, 3, 5, 7, 9}; int target = 5; int index = binarySearch(array, target); if (index != -1) { System.out.println("目标值 " + target + " 在数组中的索引位置为 " + index); } else { System.out.println("目标值 " + target + " 不存在于数组中"); } } } ``` 以上代码实现了一个名为`binarySearch`的静态方法,接受一个有序数组`array`和目标值`target`作为输入,返回目标值在数组中的索引位置。如果目标值不存在于数组中,则返回-1。 在`main`方法中,定义了一个示例数组`array = {1, 3, 5, 7, 9}`和目标值`target = 5`,然后调用`binarySearch`方法进行查找。程序将输出"目标值 5 在数组中的索引位置为 2",表示目标值5在数组中的索引位置为2。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值