2012-03-11 71 views
2

我試圖用Java實現的二進制搜索:}定製二進制搜索中的ArrayIndesOutOfBoundsException?

import java.util.Scanner; 

public class Search 
{ 
public static void sequential_search(int[] array,int search_variable) 
{ 
    boolean flag = false; 
    for(int loop=0;loop<array.length;loop++) 
    { 
     if(array[loop] == search_variable) 
     { 
      flag = true; 
      break; 
     } 
    } 
    if(flag == true) 
     System.out.println("The value is present"); 
    else 
     System.out.println("The value is absent"); 
} 

public static int binary_search_recursive(int[] array,int low,int high,int search_variable) 
{ 
    if(low > high) 
    { 
     return 0; 
    } 
    else 
    { 
     int mid; 
     mid = (low+high)/2; 
     if(array[mid] == search_variable) 
     { 
      return 1; 
     } 
     else if (array[mid] > search_variable) 
     { 
      return binary_search_recursive(array, low, mid - 1, search_variable); 
     } 
     else 
     { 
      return binary_search_recursive(array, mid + 1, high, search_variable); 
     } 
    } 
} 

public static void main(String[] args) 
{ 
    int[] array = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; 
    int search_variable; 

    System.out.println("Please enter the number to be search:"); 
    Scanner number = new Scanner(System.in); 
    search_variable = number.nextInt(); 
    System.out.println("The number to be searched is "+search_variable); 

    //sequential_search(array,search_variable); 

    if ((binary_search_recursive(array, 0, array.length, search_variable)) == 0) 
     System.out.println("The value is present"); 
    else 
     System.out.println("The value is absent"); 

} 

,而我得到了ArrayOutOfBoundsException中,我只是感到困惑,當我進入其不被發現的一個術語算法。

Please enter the number to be search: 
12 
The number to be searched is 12 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 
    at Search.binary_search_recursive(Search.java:32) 
    at Search.binary_search_recursive(Search.java:42) 
    at Search.binary_search_recursive(Search.java:42) 
    at Search.binary_search_recursive(Search.java:42) 
    at Search.main(Search.java:59) 

對此的任何輸入將有所幫助。 謝謝:)

+0

請添加行號。第32和42行是什麼? – Thilo 2012-03-11 07:04:19

+0

您是否嘗試使用調試器進行調試? – home 2012-03-11 07:04:26

回答

4

我認爲,這裏的問題是,你的lowhigh變量表示的範圍爲[低,高),而排除high值。這意味着你的基本情況,這是目前

if(low > high) 
{ 
    return 0; 
} 

也許應該

if(low >= high) 
{ 
    return 0; 
} 

因爲如果你有範圍爲[低,低),有沒有留下任何元素。在終止之前需要low > high意味着您需要低到實際超過high,這應該是不可能的。

如果我對此正確,這也意味着您對該函數的遞歸調用之一具有錯誤的上限。具體而言,此代碼:

else if (array[mid] > search_variable) 
    { 
     return binary_search_recursive(array, low, mid - 1, search_variable); 
    } 

應該

else if (array[mid] > search_variable) 
    { 
     return binary_search_recursive(array, low, mid, search_variable); 
    } 

因爲如果high是排他性的,在mid傳遞作爲上限是得到了一切,但不包括中間值的正確方法。

希望這會有所幫助!

+0

非常感謝,那是我一直在尋找的東西! – user1141584 2012-03-11 07:06:41

0
if ((binary_search_recursive(array, 0, array.length, search_variable)) == 0) 

如果您使用這些參數,您最終會檢查array [array.length]。 10個元素表示array.length爲10,但第10個索引當然是未定義的。

if ((binary_search_recursive(array, 0, array.length-1, search_variable)) == 0)