2014-02-28 92 views
0

所以我遇到了遞歸程序的問題。該程序將文本文件作爲具有未排序數字列表的輸入。然後它會要求用戶輸入一個數字,以便使用遞歸二進制搜索來查找數組。如果找到該號碼,則返回該號碼的索引,如果不是-1則返回。現在這個程序在這裏返回-1,即使它在數組中也是如此。我不明白的部分是返回mid語句被調用,然後返回-1被稱爲幾次。所以我很困惑我的程序如何運行到一個返回語句,但忽略它,然後返回-1。這是我使用的文本文件中的數字。遞歸Java程序忽略如果其他語句返回並返回3次

31 70 5 71 140 187 162 98 153 8 109 103 145 157 27 23 136 54 19 168 114 25 139 129 94 

這裏是程序本身。

import java.io.*; 
import java.util.*; 

public class Lab2 
{ 
public static void main (String[] args) throws Exception 
{ 
    if (args.length < 1) 
    { 
     System.out.println("Fatal Error. Enter a filename on the command line!\n"); 

     System.exit(0); 
    } 

    int[] arr = new int[30]; // don't do the resizing thing. Leave that to Project#1 
    int cnt=0; 

    Scanner file1 = new Scanner(new FileReader(args[0])); 
    while (file1.hasNextInt()) 
     arr[cnt++]= file1.nextInt(); 
    file1.close(); 

    // print the array as it came from the file 

    printArray("original array: ", arr, cnt); 

    // sort using Arrays.sort (see utils API) 

    Arrays.sort( arr, 0, cnt); // 2nd index non inclusive - i.e. cnt-1 

    // re-print it - should come out sorted 

    printArray("sorted array: ", arr, cnt); 

    // now search the sorted array using YOUR bSearch 

    Scanner kbd = new Scanner(System.in); 
    do 
    { 
     System.out.print("Enter number to search for: "); 
     int key = kbd.nextInt(); 
     if (key <= 0) break; // ENTER ZERO OR NEGATIVE TO QUIT LOOP 
      int index=bSearch(arr, 0, cnt-1, key); 
     if (index < 0) 
      System.out.println(key + " not found at index: " + index); 
     else 
      System.out.println(key + " found at index: " + index); 
    } 
    while (true); // infinite loop. Must break to get out 


} // END main 

// ====================================================================== 
//     M E T H O D S 
// ====================================================================== 

// return the index where key was found 
// else return -1 for not found 
static int bSearch(int[] array, int low, int high, int key) 
{ 
    int mid; 

    if (low <= high) 
    { 
     mid = (low+high)/2; 

     if (array[mid] < key) 
     { 
      bSearch(array, mid+1, high, key); 
     } 
     else if (array[mid] > key) 
     { 
      bSearch(array, low, mid-1, key); 
     } 
     else { 
      System.out.println("this is true"); 
      return mid; 
     } 
    } 
    System.out.println("why is this returning"); 
    return -1; 
} 

// USE THIS METHOD AS GIVEN: DO NOT CHANGE 

private static void printArray(String label, int[] array, int count) 
{ 
    System.out.print(label); 
    for(int i=0 ; i<count ;++i) 
     System.out.print(array[i] + " "); 
    System.out.println("\n"); 
} 

回答

1

您錯過了2個return語句。

if (array[mid] < key) 
    { 
     return bSearch(array, mid+1, high, key); 
    } 
    else if (array[mid] > key) 
    { 
     return bSearch(array, low, mid-1, key); 
    } 
+0

哇我知道我應該這樣做,但我想我從來沒有想過要檢查那部分。 – ddelnano

1

你打電話給bsearch,但你沒有回覆答案。兩條線都表示bsearch(...應該說是return bsearch(...