搜尋此網誌

2012年5月17日 星期四

Linear Search with a recursive approach.(遞迴線性搜尋)

public class RecursiveLinearSearch {
    
    public static void main(String[]args){
        
        int array [] = {1,2,3,4,5};
        findInt(array,0,3);
        
    }
    
    public static void findInt(int numbers [] , int idx , int target){
        if(idx < numbers.length){
            if(numbers[idx]==target){
                System.out.println("Found "+target + " in " + idx);
                return;
            }else{
                System.out.println("Not Found "+target + " in " + idx);
                findInt(numbers,idx+1,target);
            }    
        }
    }

}
相關文章:Linear Search

沒有留言:

張貼留言