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); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.