Reputation: 1396
I am attempting to write a function that finds the last occurrence of a target in a vector by modifying a linear search function.
private int linearSearchRecursive(int[] input, int key,int index) {
if (index == 0) {
return -1;
}
if (input[index] == key) {
return index;
}
else
return linearSearchRecursive(input,key,--index);
}
I thought of a way to make it work by using a helper function...
public static int findLastOccurance(int[] items, int key){
return linearSearchRecursive(items, key, items.length - 1);
}
Or something of that nature, but was wondering if there was an easier way where I could use only one function but keep the recursiveness?
Upvotes: 0
Views: 285
Reputation: 807
Not easier but only one function:
public class Test {
public static int findLastOccuranceRecursive(int[] input, int key, int... optionalIndex) {
if (optionalIndex.length == 0) {
optionalIndex = new int[] { input.length - 1 };
} else if (optionalIndex.length != 1) {
throw new IllegalArgumentException("size of optionalIndex must be 0 or 1");
}
if (optionalIndex[0] == 0) {
return -1;
}
if (input[optionalIndex[0]] == key) {
return optionalIndex[0];
} else {
optionalIndex[0]--;
return findLastOccuranceRecursive(input, key, optionalIndex);
}
}
public static int findLastOccuranceIterative(int[] items, int key) {
for (int i = items.length - 1; i >= 0; i--) {
if (items[i] == key) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
int[] input = { 1, 1, 1, 2, 1, 2, 1, 1 };
int testRecursive = findLastOccuranceRecursive(input, 2);
int testIterative = findLastOccuranceIterative(input, 2);
System.out.println("testRecursive: " + testRecursive + " testIterative: " + testIterative);
}
}
Upvotes: 2