Choose the code snippet which uses recursion for linear search.

Ngô Quốc Huy

New member
Choose the code snippet which uses recursion for linear search.
A.
Code:
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);
		}
	}
}
B.
Code:
      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);
			}
		}
      }
C.
Code:
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);
		}
	}
}
D.
Code:
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);
		}
	}
}
 

Tìm kiếm

Back
Top