Knowee
Questions
Features
Study Tools

Choose the code snippet which uses recursion for linear search.public void linSearch(int[] arr, int first, int last, int key){ if(first == last) { System.out.print("-1"); } else { if(arr[first] == key) { System.out.print(first); } else { linSearch(arr, first+1, last, key); } }} public void linSearch(int[] arr, int first, int last, int key) { if(first == last) { System.out.print("-1"); } else { if(arr[first] == key) { System.out.print(first); } else { linSearch(arr, first+1, last-1, key); } } }public void linSearch(int[] arr, int first, int last, int key){ if(first == last) { System.out.print("-1"); } else { if(arr[first] == key) { System.out.print(last); } else { linSearch(arr, first+1, last, key); } }}public void linSearch(int[] arr, int first, int last, int key){ if(first == last) { System.out.print("-1"); } else { if(arr[first] == key) { System.out.print(first); } else { linSearch(arr, first+1, last+1, key); } }}

Question

Choose the code snippet which uses recursion for linear search.public void linSearch(int[] arr, int first, int last, int key){ if(first == last) { System.out.print("-1"); } else { if(arr[first] == key) { System.out.print(first); } else { linSearch(arr, first+1, last, key); } }} public void linSearch(int[] arr, int first, int last, int key) { if(first == last) { System.out.print("-1"); } else { if(arr[first] == key) { System.out.print(first); } else { linSearch(arr, first+1, last-1, key); } } }public void linSearch(int[] arr, int first, int last, int key){ if(first == last) { System.out.print("-1"); } else { if(arr[first] == key) { System.out.print(last); } else { linSearch(arr, first+1, last, key); } }}public void linSearch(int[] arr, int first, int last, int key){ if(first == last) { System.out.print("-1"); } else { if(arr[first] == key) { System.out.print(first); } else { linSearch(arr, first+1, last+1, key); } }}

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The correct code snippet that uses recursion for linear search is:

public void linSearch(int[] arr, int first, int last, int key) {
    if(first == last) {
        System.out.print("-1");
    } else {
        if(arr[first] == key) {
            System.out.print(first);
        } else {
            linSearch(arr, first+1, last, key);
        }
    }
}

This code snippet correctly implements a recursive linear search. It starts from the first index of the array and checks if the current element is equal to the key. If it is, it prints the index. If it's not, it recursively calls the linSearch function, incrementing the first index by 1. This process continues until the first index is equal to the last index, at which point it prints "-1" to indicate that the key is not found in the array.

This problem has been solved

Similar Questions

Which of the following code snippet performs linear search recursively? for(i=0;i<n;i++) { if(a[i]==key) printf("element found"); } LinearSearch(int[] a, n,key) { if(n<1) return False if(a[n]==key) return True else LinearSearch(a,n-1,key) } LinearSearch(int[] a, n,key) { if(n<1) return True if(a[n]==key) return False else LinearSearch(a,n-1,key) } LinearSearch(int[] a, n,key) { if(n<1) return False if(a[n]==key) return True else LinearSearch(a,n+1,key) }

Linear search(recursive) algorithm used in _____________

Select the code snippet which performs unordered linear search iteratively?Select one:None of these optionspublic int UnorderedLinearSearch(int[] arr, int size, int data){    int index=0;    for(int i = 0; i <= size; i++)    {        if(arr[i] == data)        {            index = i;            break;        }    }    return index;}public int UnorderedLinearSearch(int[] arr, int size, int data){    int index;    for(int i = 0; i < size; i++)    {        if(arr[i] == data)        {            break;        }    }    return index;}public int UnorderedLinearSearch(int[] arr, int size, int data){    int index=0;    for(int i = 0; i < size; i++)    {        if(arr[i] == data)        {            index = i;            break;        }    }    return index;}

How does the binary search work? a. Divides an array into two smaller subarrays and applies a linear search to each of them b. None of the above c. Compares each second element with a key recursively d. Recursively goes to the left or right subarrays and compares a key with the middle e. Compares each element with a key starting from 0`s element till the last element iteratively

Given an unsorted array Arr[] of N integers and a Key which is present in this array. You need to write a program to find the start index( index where the element is first found from left in the array ) and end index( index where the element is first found from right in the array ).If the key does not exist in the array then return -1 for both start and end index in this case.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.