帮写一个二分查找算法java

时间: 2023-09-21 18:04:59 浏览: 14
public class BinarySearch { public static int binarySearch(int[] arr, int target) { int left = 0; int right = arr.length - 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; } } // 测试 public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int target = 6; int index = BinarySearch.binarySearch(arr, target); if (index == -1) System.out.println("未找到"); else System.out.println("目标元素下标为:" + index); } }

相关推荐

### 回答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) / 2; if (array[mid] == target) { return mid; } else if (array[mid] < target) { left = mid + 1; } else if (array[mid] > target) { right = mid - 1; } } return -1; } } ### 回答2: 二分查找是一种高效的查找算法,它通常应用于已排序的数组或列表中。以下是用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; } else if (array[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; // 如果找不到目标元素,则返回-1 } public static void main(String[] args) { int[] array = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; int target = 12; int result = binarySearch(array, target); if (result == -1) { System.out.println("目标元素不存在"); } else { System.out.println("目标元素在索引 " + result); } } } 在以上代码中,binarySearch方法接受一个已排序的整数数组和一个目标值作为输入,并返回目标值在数组中的索引。算法的核心思想是将查找范围不断缩小一半,直到找到目标元素或无法再继续缩小范围为止。初始时,将左边界设置为数组的第一个元素的索引,将右边界设置为数组最后一个元素的索引。然后,计算中间元素的索引并将其与目标值进行比较。如果中间元素等于目标值,则返回其索引。如果中间元素小于目标值,则将左边界更新为中间元素的索引加1。如果中间元素大于目标值,则将右边界更新为中间元素的索引减1。通过不断缩小范围,最终目标元素要么被找到并返回其索引,要么返回-1表示未找到目标元素。在本例中,目标元素12在数组中的索引为5,因此程序会打印出"目标元素在索引 5"。 ### 回答3: 二分查找算法,又称折半查找算法,是一种高效的查找算法。它通过将查找区间逐渐缩小,最终定位到目标值所在的位置,或者确定目标值不存在。 以下是使用Java语言编写的二分查找算法: java public class BinarySearch { public static int binarySearch(int[] arr, int target) { int left = 0; int right = arr.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) { return mid; // 目标值在中间位置 } if (arr[mid] < target) { left = mid + 1; // 目标值在右半部分 } else { right = mid - 1; // 目标值在左半部分 } } return -1; // 目标值不存在 } public static void main(String[] args) { int[] arr = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}; int target = 23; int index = binarySearch(arr, target); if (index != -1) { System.out.println("目标值 " + target + " 在数组中的索引位置为 " + index); } else { System.out.println("目标值 " + target + " 不存在于数组中"); } } } 上述代码中,binarySearch方法接收一个有序数组arr和目标值target作为参数,并返回目标值在数组中的索引位置,如果目标值不存在,则返回-1。 在主函数中,我们通过调用binarySearch方法实现了对有序数组的二分查找,并对查找结果进行了输出。对于给定的示例数组{2, 5, 8, 12, 16, 23, 38, 56, 72, 91},我们要查找的目标值为23。运行程序后,输出结果为"目标值 23 在数组中的索引位置为 5",这表明目标值23的确存在于数组中,并且索引位置为5。
### 回答1: 好的,这是 Java 代码实现的二分查找: public class BinarySearch { public static int binarySearch(int[] arr, int target) { int left = 0; int right = arr.length - 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; } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int target = 4; int result = binarySearch(arr, target); System.out.println("The target is located at index: " + result); } } 这个代码实现了一个简单的二分查找,如果找到了目标值,就返回目标值所在的索引;如果没有找到目标值,就返回 -1。 ### 回答2: 当然可以帮您用JAVA编写一个二分查找算法。 二分查找,也称为折半查找,是一种在有序数组中查找指定元素的算法。它的基本思想是将待查找区间的中间位置的元素与目标元素进行比较,根据比较结果选择将待查找区间缩小为前半部分或后半部分,并重复这个过程,直到找到目标元素或待查找区间为空。 下面是使用JAVA语言编写的二分查找算法示例: java public class BinarySearch { public static int binarySearch(int[] arr, int target) { int left = 0; int right = arr.length - 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; // 如果未找到目标元素,返回-1 } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int target = 5; int index = binarySearch(arr, target); if (index != -1) { System.out.println("目标元素 " + target + " 在数组中的索引为 " + index); } else { System.out.println("目标元素 " + target + " 未在数组中找到"); } } } 运行上述代码,输出为“目标元素 5 在数组中的索引为 4”。 希望以上代码对您有所帮助!如果有任何问题,请随时向我提问。 ### 回答3: 二分查找,也称为折半查找,是一种常用的查找算法。其基本思想是,将查找的范围逐渐缩小为一半,直到找到目标元素或确定目标元素不存在。 以下是用JAVA实现二分查找的代码示例: java public class BinarySearch { public static int binarySearch(int[] arr, int target) { int left = 0; int right = arr.length - 1; while (left <= right) { int mid = (left + right) / 2; if (arr[mid] == target) { return mid; } if (arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; // 表示目标元素不存在 } public static void main(String[] args) { int[] arr = {1, 3, 5, 7, 9}; // 假设数组已经按照升序排列 int target = 5; int index = binarySearch(arr, target); if (index != -1) { System.out.println("目标元素在数组中的索引位置为:" + index); } else { System.out.println("目标元素不存在于数组中"); } } } 以上代码定义了一个BinarySearch类,其中包含一个静态方法binarySearch用于实现二分查找。在main方法中,创建了一个升序排列的整型数组arr,指定了目标元素target为5。通过调用binarySearch方法,返回目标元素在数组中的索引位置(如果存在),否则返回-1。最后,根据返回的结果进行输出。
### 回答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; } } 算法的思路是: - 首先,设置两个指针 left 和 right,分别指向数组的第一个元素和最后一个元素 - 然后,在 left 和 right 之间二分查找,设中间位置的索引为 mid - 如果数组的第 mid 个元素等于目标元素,则返回 mid - 如果数组的第 mid 个元素小于目标元素,则在数组的右半部分继续二分查找,设置 left = mid + 1 - 如果数组的第 mid 个元素大于目标元素,则在数组的左半部分继续二分查找,设置 right = mid - 1 - 如果在查找过程中 left 大于 right,则表示数组中没有目标元素,返回 -1 注意:这段代码假定了数组是有序的。 ### 回答2: 二分查找是一种常用的查找算法,主要用于在有序数组中查找目标元素。它的基本思想是不断将数组的中间元素与目标元素进行比较,根据比较结果确定目标元素位于左半部分还是右半部分,然后将搜索范围缩小一半,直到找到目标元素或者搜索范围为空。 下面是一个使用二分查找算法在有序数组中查找目标元素的java实现: java public class BinarySearch { public static int binarySearch(int[] nums, int target) { int left = 0; int right = nums.length - 1; while (left <= right) { int mid = (left + right) / 2; if (nums[mid] == target) { return mid; // 目标元素找到,返回索引 } else if (nums[mid] < target) { left = mid + 1; // 目标元素在右半部分,调整左边界 } else { right = mid - 1; // 目标元素在左半部分,调整右边界 } } return -1; // 目标元素未找到 } public static void main(String[] args) { int[] nums = {1, 3, 5, 7, 9}; int target = 5; int result = binarySearch(nums, target); if (result == -1) { System.out.println("目标元素未找到"); } else { System.out.println("目标元素位于索引 " + result); } } } 在这段代码中,binarySearch函数接收一个有序数组和目标元素作为参数,使用while循环进行二分查找。通过不断调整左右边界,最终找到目标元素的索引或者返回-1表示目标元素未找到。 以上就是二分查找的java代码实现。它的时间复杂度为O(log n),非常高效。 ### 回答3: 二分查找(Binary Search)是一种查找算法,它在有序数组中查找给定的目标值。 /** * 二分查找算法 * @param arr 待查找的有序数组 * @param target 目标值 * @return 目标值在数组中的索引,若不存在则返回-1 */ public static int binarySearch(int[] arr, int target) { int left = 0; // 左边界 int right = arr.length - 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; // 目标值不存在于数组中 } // 测试 public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int target = 6; int result = binarySearch(arr, target); if (result == -1) { System.out.println("目标值不存在于数组中"); } else { System.out.println("目标值在数组中的索引为:" + result); } } 以上是一个简单的Java二分查找算法。算法首先确定左右边界,然后在每次循环中计算中间元素的索引,接着根据中间元素与目标值的比较结果缩小边界,直到找到目标值或边界交叉。若找到目标值则返回其索引,否则返回-1表示目标值不存在于数组中。 在测试中,我们使用了一个有序数组arr和目标值target,然后调用binarySearch方法进行查找。最后根据返回结果输出相应的提示信息。
### 回答1: 我可以为你提供一个简单的java二分查找程序的示例: public class BinarySearchExample { public static void main(String[] args) { int[] arr = {2, 4, 6, 8, 10, 12, 14, 16}; int searchElement = 14; int lowIndex = 0; int highIndex = arr.length - 1; while (lowIndex <= highIndex) { int middleIndex = (lowIndex + highIndex) / 2; if (arr[middleIndex] == searchElement) { System.out.println("找到元素:" + searchElement + ",下标为:" + middleIndex); break; } else if (arr[middleIndex] < searchElement) { lowIndex = middleIndex + 1; } else { highIndex = middleIndex - 1; } } } } ### 回答2: 下面是一个用Java实现的二分查找程序: java public class BinarySearch { public static int binarySearch(int[] arr, int target) { int left = 0; int right = arr.length - 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 } public static void main(String[] args) { int[] arr = {10, 15, 20, 25, 30, 35, 40}; int target = 25; int index = binarySearch(arr, target); if (index != -1) { System.out.println("目标元素 " + target + " 在数组中的下标为:" + index); } else { System.out.println("目标元素 " + target + " 不存在于数组中"); } } } 此程序实现了二分查找算法,其中 binarySearch 方法接收一个已排序的整型数组 arr 和目标元素 target,返回目标元素在数组中的索引。如果目标元素不存在于数组中,则返回 -1。 程序通过不断缩小查找范围,每次取中点进行比较,直到找到目标元素或范围为空为止。 在 main 方法中,我们定义了一个示例数组 arr 和目标元素 target,然后调用 binarySearch 方法进行查找,并根据返回结果输出相应的信息。 以上就是一个简单的Java二分查找程序的实现。 ### 回答3: 下面是一个用Java编写的二分查找程序: java public class BinarySearch { public static int binarySearch(int arr[], int target) { int left = 0; int right = arr.length - 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; } public static void main(String args[]) { int arr[] = {1, 3, 5, 7, 9}; int target = 5; int result = binarySearch(arr, target); if (result == -1) { System.out.println("目标元素未找到"); } else { System.out.println("目标元素在数组中的索引位置为 " + result); } } } 这个程序定义了一个BinarySearch类,其中有一个binarySearch方法用于执行二分查找。在main方法中,我们定义了一个有序数组arr和一个目标元素target,然后调用binarySearch方法进行查找。如果目标元素存在于数组中,该方法会返回它的索引位置;否则返回-1。最后,我们根据结果输出相应的信息。在上述例子中,目标元素5存在于数组中并返回其索引位置为2。
### 回答1: 我可以为你编写一个Java二分查找程序。它需要一个数组,一个搜索值,以及一个比较器(可选),并且返回搜索值的索引(如果存在)或-1(如果不存在)。以下是一个示例: public static int binarySearch(int[] array, int value, Comparator comp) { int low = 0; int high = array.length - 1; while (low <= high) { int mid = (low + high) / 2; int midValue = array[mid]; if (comp.compare(midValue, value) < 0) { low = mid + 1; } else if (comp.compare(midValue, value) > 0) { high = mid - 1; } else { return mid; } } return -1; } ### 回答2: 二分查找,也称为折半查找,是一种高效的查找算法。它的思想是,首先将有序数组的中间元素与待查找的元素进行比较,如果相等,则查找成功;如果不相等,则判断待查找元素与中间元素的大小关系,如果待查找元素大于中间元素,则在右半部分继续查找,否则在左半部分继续查找,直到找到目标元素或者搜索范围为空。 下面是一个简单的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; // 如果未找到目标元素,返回-1 } public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int target = 6; int result = binarySearch(array, target); if (result == -1) { System.out.println("未找到目标元素"); } else { System.out.println("目标元素的索引为:" + result); } } } 以上代码定义了一个BinarySearch类,其中binarySearch方法接收一个有序数组和目标元素作为参数,并返回目标元素在数组中的索引,如果未找到目标元素则返回-1。在main方法中创建了一个有序数组array和待查找的目标元素target,然后调用binarySearch方法进行查找,并根据返回结果输出查找结果。 这是一个基本的二分查找实现,它的时间复杂度为O(log n),适用于对有序数组进行查找的场景。 ### 回答3: 当然,请看下面的代码: java public class BinarySearch { public static int binarySearch(int[] nums, int target) { int left = 0; int right = nums.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) { return mid; } if (nums[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; } public static void main(String[] args) { int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int target = 4; int index = binarySearch(nums, target); if (index != -1) { System.out.println("目标元素在数组中的索引为:" + index); } else { System.out.println("目标元素不在数组中"); } } } 上面的代码是一个简单的Java二分查找实现。它接收一个已排序的整数数组和目标元素作为参数,返回目标元素在数组中的索引(如果存在),否则返回-1。首先,设定左边界left为数组的第一个元素的索引,右边界right为数组的最后一个元素的索引。然后,在一个循环中,将中间元素的索引计算为(left + right) / 2。如果中间元素等于目标元素,那么立即返回它的索引。否则,如果中间元素小于目标元素,将左边界移动到中间元素的右侧,并继续搜索右半部分。如果中间元素大于目标元素,将右边界移动到中间元素的左侧,并继续搜索左半部分。如果最终没有找到目标元素,则返回-1。 在main方法中,我们定义了一个整数数组nums和一个目标元素target,然后调用binarySearch方法进行搜索。最后,根据返回的索引值输出结果。在这个例子中,目标元素4在数组中的索引为3。

最新推荐

求猴子大王JAVA代码

题目描述:求猴子大王 15个猴子围成一圈选大王,依次1-7循环报数,报到7的猴子被淘汰,直到最后一只猴子称为大王,问:哪只猴子会成为大王?

比较行业增长率趋势.xlsx

比较行业增长率趋势.xlsx

前端在线应用工具大全.doc

json随机生成工具 xml随机生成工具 前端开发快捷键 网页设计常用色彩搭配表 48色蜡笔颜色,彩铅色彩 180款常用渐变色 配色大全 在线字体查看器(支持iconfont/woff) 任意文件转base64 base64还原成文件 SVG压缩工具 图床 在线html转js,js转html fontawesome图标在线查询 在线获取键盘按键值(keycode,ascii码) 字符生成线条字 图片压缩工具 生成音乐播放器 在线photoshop 在线代码编辑器 在线生成圆角 ICO图标在线生成转换工具 IOS安卓logo在线生成器 ueditor在线代码编辑器 RunJS在线编辑器 WEB安全色 在线调色板 中国传统色彩 HTML5兼容性测试 CSS3贝塞尔曲线工具 CSS3关键帧动画模板 CSS3过渡动画模板等等

金融大数据解决方案.pptx

金融大数据解决方案.pptx

2023年3月重卡销量报告(总批发量)-商用车.pptx

2023年3月重卡销量报告(总批发量)-商用车.pptx

基于51单片机的usb键盘设计与实现(1).doc

基于51单片机的usb键盘设计与实现(1).doc

"海洋环境知识提取与表示:专用导航应用体系结构建模"

对海洋环境知识提取和表示的贡献引用此版本:迪厄多娜·察查。对海洋环境知识提取和表示的贡献:提出了一个专门用于导航应用的体系结构。建模和模拟。西布列塔尼大学-布雷斯特,2014年。法语。NNT:2014BRES0118。电话:02148222HAL ID:电话:02148222https://theses.hal.science/tel-02148222提交日期:2019年HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire论文/西布列塔尼大学由布列塔尼欧洲大学盖章要获得标题西布列塔尼大学博士(博士)专业:计算机科学海洋科学博士学院对海洋环境知识的提取和表示的贡献体系结构的建议专用于应用程序导航。提交人迪厄多内·察察在联合研究单位编制(EA编号3634)海军学院

react中antd组件库里有个 rangepicker 我需要默认显示的当前月1号到最后一号的数据 要求选择不同月的时候 开始时间为一号 结束时间为选定的那个月的最后一号

你可以使用 RangePicker 的 defaultValue 属性来设置默认值。具体来说,你可以使用 moment.js 库来获取当前月份和最后一天的日期,然后将它们设置为 RangePicker 的 defaultValue。当用户选择不同的月份时,你可以在 onChange 回调中获取用户选择的月份,然后使用 moment.js 计算出该月份的第一天和最后一天,更新 RangePicker 的 value 属性。 以下是示例代码: ```jsx import { useState } from 'react'; import { DatePicker } from 'antd';

基于plc的楼宇恒压供水系统学位论文.doc

基于plc的楼宇恒压供水系统学位论文.doc

"用于对齐和识别的3D模型计算机视觉与模式识别"

表示用于对齐和识别的3D模型马蒂厄·奥布里引用此版本:马蒂厄·奥布里表示用于对齐和识别的3D模型计算机视觉与模式识别[cs.CV].巴黎高等师范学校,2015年。英语NNT:2015ENSU0006。电话:01160300v2HAL Id:tel-01160300https://theses.hal.science/tel-01160300v22018年4月11日提交HAL是一个多学科的开放获取档案馆,用于存放和传播科学研究文件,无论它们是否已这些文件可能来自法国或国外的教学和研究机构,或来自公共或私人研究中心。L’archive ouverte pluridisciplinaire博士之路博士之路博士之路在获得等级时,DOCTEURDE L'ÉCOLE NORMALE SUPERIEURE博士学校ED 386:巴黎中心数学科学Discipline ou spécialité:InformatiquePrésentée et soutenue par:马蒂厄·奥布里le8 may 2015滴度表示用于对齐和识别的Unité derechercheThèse dirigée par陪审团成员équipe WILLOW(CNRS/ENS/INRIA UMR 8548)慕尼黑工业大学(TU Munich�