Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a recursive , linear/sequential search that takes advantage of an array be

ID: 3741761 • Letter: W

Question

Write a recursive, linear/sequential search that takes advantage of an array being sorted.

Optimize by stopping the search either if you reach the end of the array or you know that the element cannot be in the array.

Note: this is a linear search- not a binary search!

The data is sorted.

The method header is:

public boolean searchRecursiveLinearSorted(Comparable[] array, Comparable target)

Explanation / Answer

public boolean searchRecursiveLinearSorted(Comparable[] array, Comparable target) { return searchRecursiveLinearSorted(array, target, 0); } public boolean searchRecursiveLinearSorted(Comparable[] array, Comparable target, int index) { if(index == array.length || array[index].compareTo(target) > 0) { return false; } else { return array[index].compareTo(target) == 0 || searchRecursiveLinearSorted(array, target, index+1); } }