Question

Write and implement a recursive version of the sequential search algorithm. Test the algorithm with an...

Write and implement a recursive version of the sequential search algorithm. Test the algorithm with an array that have ten integer values, user can enter the values or assigned the values to the array in the main method. The search key should be a value in the array or not in the array. JAVA!!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class LinearSearch {
   public static void main(String[] args) {
       int arr[]= {10,24,12,5,26,33,45,21};
       int key=21;
       int index=linear_search_rec(arr, arr.length-1, key);
       if(index==-1) {
           System.out.println(key+" does not exist in array");
       }
       else {
           System.out.println(key+" found at index "+index);
       }
      
       key=29;
       index=linear_search_rec(arr, arr.length-1, key);
       if(index==-1) {
           System.out.println(key+" does not exist in array");
       }
       else {
           System.out.println(key+" found at index "+index);
       }
   }
   public static int linear_search_rec(int arr[], int n ,int key){
       if(n<0) { // Base case - not found
       return -1;
       }
       if(arr[n]==key) { // Base case - found
       return n;
       }
       // Recursive case
       return linear_search_rec(arr, n-1, key);
       }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
Write and implement a recursive version of the sequential search algorithm. Test the algorithm with an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT