简体   繁体   English

如何对数组中的一个特定元素进行二进制搜索?

[英]how to binary search one specific element in an array?

i have a binary search algorithm set up but i didn't know how to make it work like what where im suppose to tell it to look for an element and show if it is found or not any tip would help thank you我设置了一个二分搜索算法,但我不知道如何让它像我想告诉它寻找一个元素并显示它是否找到的那样工作,任何提示都会有所帮助,谢谢

public static int search(int arr[], int x)
{ 
    int startIndex = 0 ; 
    int endIndex = arr.length-1;
    while ( startIndex <=endIndex){
    int midpoint = (startIndex + endIndex )/2;
    if(arr[midpoint]==x)
        return midpoint;
    else if(arr[midpoint]<x)
        startIndex=midpoint+1;
    else
        endIndex = midpoint = -1;
    }
    return -1;
}
 
//here i want to make it search for 6 

    public static void main (String [] args ){
     search v = new search();
     int [] test = {1,99,6,32,4,6,33,90};
     for (int element: test) {
    System.out.println("the elements of array: "+ element);
    int x = 6;
    int result=v.binarySearch();
     }

Binary Search works as, Search a sorted array by repeatedly dividing the search interval in half.二分搜索的工作原理是,通过重复将搜索间隔分成两半来搜索排序数组。 Begin with an interval covering the whole array.从覆盖整个数组的间隔开始。 If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half.如果搜索键的值小于区间中间的项,则将区间缩小到下半部分。 Otherwise, narrow it to the upper half.否则,将其缩小到上半部分。 Repeatedly check until the value is found or the interval is empty.反复检查,直到找到值或区间为空。

    int[] test = {1, 99, 6, 32, 4, 6, 33, 90};
    int num = 6;
    Arrays.sort(test);
    System.out.println(binarySearch(test, num, 0, test.length));

logic for the binary search is given below as,二进制搜索的逻辑如下所示,

public int binarySearch(int arr[], int num, int min, int max) {
    if (min > max) {
      return -1;
    }
    int mid = min + (max - min) / 2;
    if (arr[mid] == num) {
      return mid;
    } else if (arr[mid] < num) {
      return binarySearch(arr, num, mid + 1, max);
    }
    return binarySearch(arr, num, min, mid - 1);
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在二进制搜索中找到数组的最后一个元素 - How to find the last element of array in binary search 如何在包含特定年份的对象数组中二进制搜索特定年份 - How to binary search for a specific year in an array of objects containing a specific year 如何使用具有此特定公式的未排序数组实现二叉搜索树? - How to implement Binary Search Tree using an unsorted array with this specific formula? 如何使用二进制搜索来查找具有一定权重的数组中的第一个元素(另一个数组中的另一个元素)? - How to use binary search in order to find the first element in an array that has certain weight (another element in a different array)? 在基于数组的二叉搜索树中添加元素 - Adding an element in an array based Binary Search Tree 我的基本二进制搜索方法不适用于Java中int数组中的一个特定值 - My basic binary search method does not work for one specific value in an int array in Java 使用Binary Search Recursion将元素的插入位置返回到排序数组中 - Return the insert position of an element into a sorted array with Binary Search Recursion 使用二分搜索查找数组中的一个元素,使其索引等于其值 - Finding an element in an array such that the index is equal to its value using binary search Java:递归二进制搜索以查找数组的元素-逻辑错误 - Java:Recursion Binary search to find element of an array-Logical error 为什么这个二进制搜索返回-1,即使该元素在数组中 - Why does this binary search return -1 even though the element is in the array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM