在java中进行二进制搜索

时间:2012-10-15 05:59:09

标签: java arrays search binary

我正在尝试用Java实现二进制搜索,不起作用...不知道为什么,它总是给我一个错误,说没有找到数字......

我不知道为什么,我没有看到任何错误:谢谢你的帮助......

public void busquedaBinaria(int[] arreglo, int buscar) {        

        int centro = 0; //middle
        int inferior = 0;
        int superior = arreglo.length - 1;
        boolean encontrado = false; //found flag

        while(inferior <= superior)
        {
            centro = (superior + inferior) / 2;            

              if (arreglo[centro] == buscar){
                  System.out.println("-Number " + buscar + " found in the " + centro + " position.");
                  encontrado=true;
                  break;
              }
              else if (arreglo[centro] > buscar) {
                  superior = centro - 1;
              }
              else{
                  inferior = centro + 1;
              }     
              System.out.println(centro);
        }

        if (encontrado == false) {
            System.out.println("-Number " + buscar + " hasn't been found.");
        }
    }

4 个答案:

答案 0 :(得分:4)

您可以使用库中已有的Arrays#binarySearchCollections#binarySearch

答案 1 :(得分:1)

二进制搜索仅适用于已排序的数字。请确保数组 arreglo 已排序。

如果您的阵列处于降序,那么您必须更改逻辑位。

     if (arreglo[centro] == buscar){
       System.out.println("-Number " + buscar + " found in the " + centro + " position.");
       encontrado=true;   
       break; 
     } 
     else if (arreglo[centro] > buscar) { 
       superior = centro + 1;
     }
     else{ 
       inferior = centro - 1; 
     }      

答案 2 :(得分:1)

这是我的贡献:

public class BinarySearch
{
   private static String search( String[] entries, String entry, int left, int right )
   {
      int index = ( left + right ) / 2;
      if( left > right || index >= entries.length )
      {
         return null;
      }
      int cmp   = entries[index].compareTo( entry );
      if( cmp == 0 )
      {
         return entry;
      }
      if( cmp > 0 )
      {
         return search( entries, entry, left, index - 1 );
      }
      return search( entries, entry, index + 1, right );
   }// String search( String[] entries, String entry, int left, int right )



   public static String search( String[] entries, String entry )
   {
      return search( entries, entry, 0, entries.length );
   }// String search( String[] entries, String entry )



   public static void main( String[] args )
   {
      String[] entries = new String[] { "Aubin", "Barbara", "George", "Muriel", "Sarah", "Tom" };
      for( String entry : entries )
      {
         System.out.println( search( entries, entry ));
      }
      System.out.println( search( entries, "Toto" ));
   }

}

答案 3 :(得分:0)

另一种在java中实现二分查找的方法。

int leftIndex = 0;
int rightIndex = array.length - 1;
    while (leftIndex <= rightIndex) {
        // find the middle Index
        int middleIndex = (leftIndex + rightIndex) / 2;
        if (array[middleIndex] == target) {
            return middleIndex;
        }
        if (target > array[middleIndex]) {
            // ignoring the left side of array
            leftIndex = middleIndex + 1;
        } else {
            // ignoring the right side of array
            rightIndex = middleIndex - 1;
        }
    }
        return -1;

Sample 数组如下所示

enter image description here

更多参考:

https://beginnersbug.com/binary-search-algorithm/