Page 411 - Cs_withBlue_J_C11_Flipbook
P. 411
20 {
21 if(i==n) // not found base case
22 return -1;
23 else if(arr[i]==v) // found base case
24 return i;
25 else
26 return linear(i+1,v); // recursive case
27 }
28
29 public static void main(int s,int v)
30 {
31 LSearch obj = new LSearch(s);
32 obj.fillarray();
33 int p=obj.linear(0,v);
34 if(p==-1)
35 System.out.println(v+"not found in array ");
36 else
37 System.out.println(v+" found in location: " +(p+1));
38 }
39 }
The output of the preceding program is as follows:
Enter 7 elements
12 23 34 67 89 90 22
89 found in location: 5
409
Recursion 409

