2015-05-01 90 views
-2

在我的代碼中,我正在處理我有一個二進制搜索,它是假設找到具體的數字,但現在我無法弄清楚爲什麼它告訴我沒有找到每一個數字。我正在嘗試使用遞歸。遞歸BinarySearch問題

public class BinarySearch { 

    private static boolean binarySearch(int[] myList, int numberToFind) { 
     // So this will be your recursive method. 
     // Right now it just returns false. 
     // But you need to change this code. 
     return false; 
    } 

    public static void main(String[] args) { 
     // Create an array of sorted numbers 

     int[] evenList = 
      { 2, 4, 9, 11, 17, 19, 22, 29, 30, 33, 
       39, 43, 46, 47, 51, 52, 54, 56, 58, 59, 
       63, 69, 70, 79, 88, 89, 92, 96, 98, 99 }; 

     // Can we find every number? 
     for (int i = evenList.length -1; i >= 0; i--) { 
      if (binarySearch(evenList, evenList[i])) 
       System.out.printf("%d was found.\n\n", evenList[i]); 
      else 
       System.out.printf("%d was not found.\n\n", evenList[i]); 
     } 
     // Will we not find these numbers? 
     int[] testCases = { 1, 44, 100, 32 }; 
     for (int i = 0; i > testCases.length; i--) { 
      if (binarySearch(evenList, testCases[i])) 
       System.out.printf("%d was found.\n\n", testCases[i]); 
      else 
       System.out.printf("%d was not found.\n\n", testCases[i]); 
     } 
    } 
} 
+0

我可以告訴你爲什麼它找不到任何數字...... –

+0

閱讀包含註釋的代碼將是一個不錯的第一步。 –

+0

我也可以。它在評論中。 –

回答

1

那麼看看這個代碼

private static boolean binarySearch(int[] myList, int numberToFind) { 
    // So this will be your recursive method. 
    // Right now it just returns false. 
    // But you need to change this code. 
    return false; 

你需要實現它的工作之前,該方法。

+0

我覺得真的很愚蠢,因爲錯過了我將它發送給某人,並且他們發回了一無所有,但我從未看過評論的代碼謝謝 –