@@ -10,10 +10,39 @@ static String linearSearch(int a[],int n,int t){
1010 return "Founded" ;
1111 return "NotFounded" ;
1212 }
13+
14+ static boolean recursiveLinearSearch (int a [],int index , int target ) {
15+ if (index == a .length -1 )
16+ return false ;
17+ return a [index ]==target || recursiveLinearSearch (a , index +1 , target );
18+ }
19+
20+
21+ //Example a[] = {1,23,5,5,10}
22+ static int recursiveLinearSearchForward (int a [],int index , int target ) {
23+ if (index == a .length -1 )
24+ return -1 ;
25+ if (a [index ]==target )
26+ return index ;
27+ return recursiveLinearSearchForward (a , index +1 , target );
28+ } //it return index as 2
29+ static int recursiveLinearSearchBackward (int a [],int index , int target ) {
30+ if (index == -1 )
31+ return -1 ;
32+ if (a [index ]==target )
33+ return index ;
34+ return recursiveLinearSearchBackward (a , index -1 , target );
35+ } //it return index as 3
36+
37+ // if we have multiple occurs in search array if you need two value index added in the new array list
38+
1339 public static void main (String [] args ) {
14- int a []={11 ,3 ,55 ,6 ,7 ,355 ,77 };
15- int target =15 ;
40+ int a []={11 ,3 ,55 ,55 , 6 ,7 ,355 ,77 };
41+ int target =55 ;
1642 int n =a .length ;
1743 System .out .println (linearSearch (a ,n ,target ));
44+ System .out .println (recursiveLinearSearch (a ,0 ,target ));
45+ System .out .println (recursiveLinearSearchForward (a ,0 ,target ));
46+ System .out .println (recursiveLinearSearchBackward (a ,n -1 ,target ));
1847 }
1948}
0 commit comments