2013-04-09 153 views
0

JAVA作業二進制遞歸搜索

有人能給我一些關於我在做什麼錯誤的指針嗎?謝謝。

16.編寫練習11(f)的遞歸解決方案,二進制搜索數組以找到值a Key。

public class BinarySearch { 
    public static void main(String[] args) { 
     public static int BinarySearch(int[] sorted, int first, int upto, int key) { 
      if (first < upto) { 
       int mid = first + (upto - first)/2; // Compute mid point. 
       if (key < sorted[mid]) { 
        return BinarySearch(sorted, first, mid, key); 
       } else if (key > sorted[mid]) { 
        return BinarySearch(sorted, mid+1, upto , key); 
       } else { 
        return mid; // Found it. 
       } 
      } 
      return -(first + 1); // Failed to find key 
     } 
    } 
} 
+0

而是'第一,中期',你可以使用'第一,中1' – 2013-04-09 16:11:39

回答

2

的問題是,你定義內的另一個方法的方法:

public static void main(String[] args) { 
    public static int BinarySearch(int[] sorted, int first, int upto, int key) { 

只需推動BinarySearch方法main方法外:

public static void main(String[] args) { 
    //content of this method 
} 

public static int BinarySearch(int[] sorted, int first, int upto, int key) { 
    //content of this other method 
}