This one is making my head spin. Just when I think I got it, I realize something's not right. I have to use recursion for this assignment. Any hints?
/**
* Uses recursion to find index of the shortest string.
* Null strings are treated as infinitely long.
* Implementation notes:
* The base case if lo == hi.
* Use safeStringLength(paths[xxx]) to determine the string length.
* Invoke recursion to test the remaining paths (lo +1)
*/
static int findShortestString(String[] paths, int lo, int hi) {
int min=lo;
if (lo==hi)
return min;
if (safeStringLength(paths[lo]) < safeStringLength(paths[lo+1])){
min=lo;
return Math.min(min, findShortestString(paths, lo+1, hi));
}
else{
min=lo+1;
return Math.min(min, findShortestString(paths, lo+1, hi));
}
}
I think got something here:
static int findShortestString(String[] paths, int lo, int hi)
{
if (lo==hi)
return lo;
int ShortestIndexSoFar = findShortestString(paths, lo+1, hi);
if(safeStringLength(paths[ShortestIndexSoFar]) < safeStringLength(paths[lo]))
return ShortestIndexSoFar;
else
return lo;
}
static int safeStringLength(String str)
{
if(str == null)
return Integer.MAX_VALUE;
return str.length();
}
Explaining why this works:
Here's a sample:
[0] ab
[1] abcd
[2] a
[3] abc
[4] ab
Obviously, index 2 is the shortest one.
Think bottoms up. Read the following starting from the bottom, upwards.
I make it sound like each function is talking to the function above it in the chain.
And each line is lead by the function parameters that were passed.
"[0] ab (paths, 0, 4): return 2, coz he's shorter than me or anyone before us"
"[1] abcd (paths, 1, 4): return 2, coz he's shorter than me or anyone before us"
"[2] a (paths, 2, 4): return 2, I'm shorter than anyone before me"
"[3] abc (paths, 3, 4): return 4, coz he's shorter than me or anyone before us"
"[4] ab (paths, 4, 4): return 4, I'm the shortest; I don't know any better"
So in the code, you see that exactly happening.
When we define ShortestIndexSoFar, this is where each function will know the shortest of all the paths beyond it.
And right after it is where the function itself checks if its index has a shorter path than the shortest of all the ones below.
Keep trickling the shortest one upward, and the final guy will return the shortest index.
That makes sense?
Since this is homework, here's a hint to help you learn:
The signature of the findShortestString method suggests that you should be using a binary search. Why would I say that? Why would it be a good idea to do that? All of the other solutions suffer from a practical problem in Java ... what would that be?
To people other than the OP ... PLEASE DON'T GIVE THE ANSWERS AWAY ... e.g. by correcting your answers!
Why not just get the length of each element and sort the returned length to get the ordering? Like this.
int[] intArray = {10, 17, 8, 99, 1}; // length of each element of string array
Arrays.sort(intArray);
one solution will be like this
public static final int findShortestString(final String[] arr, final int index, final int minIndex) {
if(index >= arr.length - 1 ) {
return minIndex;
}
if(-1 == safeStringLength(arr[index])) {
return index;
}
int currentMinIncex = minIndex;
if(safeStringLength(arr[minIndex]) > safeStringLength(arr[index+1])){
currentMinIncex = index + 1;
}
return findShortestString(arr, index + 1, currentMinIncex);
}
public static final int safeStringLength(final String string) {
if( null == string) return -1;
return string.length();
}
A simple solution:
/**
* Uses recursion to find index of the shortest string.
* Null strings are treated as infinitely long.
* Implementation notes:
* The base case if lo == hi.
* Use safeStringLength(paths[xxx]) to determine the string length.
* Invoke recursion to test the remaining paths (lo +1)
*/
static int findShortestString(String[] paths, int lo, int hi) {
if (lo==hi)
return lo;
if (paths[lo] == null)
return findShortestString(paths, lo+1, hi);
int bestIndex = findShortestString(paths, lo+1, hi);
if (safeStringLength[lo] < safeStringLength[bestIndex])
return lo;
return bestIndex;
}
Calculating min on the result of running findShortestString isn't meaningful. The best way to start this kind of problem is to consider just a single recursive step, you can do this by considering what happens with an array of only two strings to compare.
What you want to do is check the length of the first string against the length of the second. The real trick, though, is that you want to test the length of the second by calling the function recursively. This is straight forward enough, but requires determining the end-case of your recursion. You did this successfully, it's when lo == hi. That is, when lo == hi the shortest known string is lo (it's the only known string!).
Ok, so back to comparing just two strings. Given that you know that you want to compare the length of two strings stored in paths, you might do something like this (without recursion):
if(safeStringLength(paths[0]) < safeStringLength(paths[1])){
return 0; // paths[0] is shorter
}else{
return 1; // paths[1] is shorter
}
But you want to recurse -- and in the recurse step you need to somehow generate that 1 of paths[1]. We already figured out how to do that, when lo == hi, we return lo. Thus the recursion step is "compare the current lowest known string length to the string length of the best known index" -- wait, we have a function for that! it's findShortestString. Thus we can modify what's written above to be slightly more concise, and add in the base case to get:
static int findShortestString(String[] paths, int lo, int hi) {
// base case, no comparisons, the only known string is the shortest one
if(lo == hi){
return lo;
}
int bestIndex = findShortestString(paths, lo+1, hi);
return safeStringLength(paths[lo]) < safeStringLength(paths[bestIndex]) ?
lo : bestIndex;
}
static int findShortestString(String[] paths, int lo, int hi)
{
if (lo==hi)
return lo;
int ShortestIndexDown = findShortestString(paths, lo, (hi + lo)/2);
int ShortestIndexUp = findShortestString(paths, (lo+hi)/2+1, hi);
return SafeStringLength(paths[ShortestIndexDown]) < SafeStringLength(paths[ShortestIndexUp])?ShortestIndexDown:ShortestIndexUp;
}
static int safeStringLength(String str)
{
if(str == null)
return Integer.MAX_VALUE;
return str.length();
}
Related
I am trying to implement a min heap in java which sorts based on two parameters. Each element of the min heap is an object which contains an int and a string. My current implementation sorts solely based on the integer but I also need it to sort in alphabetical order. For example, if the contents of the objects are as follows:
{ (stopped, 3), (anywhere, 1), (food, 17), (get, 3), (done, 1)}
the output when removing elements from the heap must be:
{(anywhere, 1), (done, 1), (get, 3), (stopped, 3), (food, 17)}
My sink and swim functions are described below:
private void swim(int n){
while (n > 1 && greater(n/2, n)){
exchange(n, n/2);
n = n/2;
}
}
private boolean greater(int i, int j){
return elements[i].getValue() >= elements[j].getValue();
}
private void exchange(int i, int j){
Node tmp = elements[i];
elements[i] = elements[j];
elements[j] = tmp;
}
private void sink(int k){
while(2*k <=n){
int i = 2*k;
if(i < n && greater(i, i+1)) i++;
if(!greater(k,i)) break;
exchange(k,i);
k = i;
}
}
Any help would be greatly appreciated!
Update
Thank you very much to #AlbertoSinigaglia, your solution worked!
you just need to update the greater method in this way:
return /*1*/ elements[i].getValue()>elements[j].getValue
||
/*2*/ (elements[i].getValue()==elements[j].getValue() && elements[i].getString().compareTo(elements[j].getString())>0)
With 1 you check if the int Value is greater, if yes, well ends there, if else it's not, it should be o = or < and we need to take care of the = case, so if the Values are equals, then we compare the String with the compareTo() method, which will return >0 in case the first String is greater than the second string
This is what I have so far, but I'm confused on how to keep track of the index. I would change the parameters of the method, but I'm not allowed.
I can only use a loop to make another array. Those are the restrictions.
public class RecursiveFinder {
static int checkedIndex = 0;
static int largest = 0;
public static int largestElement(int[] start){
int length = start.length;
if(start[length-1] > largest){
largest = start[length-1];
int[] newArray = Arrays.copyOf(start, length-1);
largestElement(newArray);
}
else{
return largest;
}
}
/**
* #param args
*/
public static void main(String[] args) {
int[] array1 = {0,3,3643,25,252,25232,3534,25,25235,2523,2426548,765836,7475,35,547,636,367,364,355,2,5,5,5,535};
System.out.println(largestElement(array1));
int[] array2 = {1,2,3,4,5,6,7,8,9};
System.out.println(largestElement(array2));
}
}
Recursive method doesn't need to keep the largest value inside.
2 parameters method
Start to call with:
largestElement(array, array.length-1)
Here is the method:
public static int largestElement(int[] start, int index) {
if (index>0) {
return Math.max(start[index], largestElement(start, index-1))
} else {
return start[0];
}
}
The 3rd line of method is the hardest one to understand. It returns one of two elements, larges of the one of current index and of remaining elements to be checked recursively.
The condition if (index>0) is similar to while-loop. The function is called as long as the index remains positive (reaches elements in the array).
1 parameter method
This one is a bit tricky, because you have to pass the smaller array than in the previous iteration.
public static int largestElement(int[] start) {
if (start.length == 1) {
return start[0];
}
int max = largestElement(Arrays.copyOfRange(start, 1, start.length));
return start[0] > max ? start[0] : max;
}
I hope you do this for the study purposes, actually noone has a need do this in Java.
Try that for the upper class, leave the main method it's is correct.
public class dammm {
public static int largestElement(int[] start){
int largest = start[0];
for(int i = 0; i<start.length; i++) {
if(start[i] > largest){
largest = start[i];
}
}return largest;
}
If your goal is to achieve this by using recursion, this is the code that you need. It is not the most efficient and it is not the best way to deal with the problem but it is probably what you need.
public static int largestElement(int[] start){
int length = start.length;
if (start.lenght == 1){
return start[0];
} else {
int x = largestElement(Arrays.copyOf(start, length-1))
if (x > start[length-1]){
return x;
} else {
return start[length-1];
}
}
}
Imagine that you have a set of numbers you just have to compare one number with the rest of them.
For example, given the set {1,8,5} we just have to check if 5 is larger than the largest of {1,8}. In the same way you have to check if 8 is larger than the largest of {1}. In the next iteration, when the set one have one value, you know that that value is the bigger of the set.
So, you go back to the previous level and check if the returned value (1) is larger than 8. The result (8) is returned to the previous level and is checked against 5. The conclusion is that 8 is the larger value
One parameter, no copying. Tricky thing is, we need to pass a smaller array to the same method. So a global variable is required.
// Number of elements checked so far.
private static int current = -1;
// returns the largest element.
// current should be -1 when user calls this method.
public static int largestElement(int[] array) {
if (array.length > 0) {
boolean resetCurrent = false;
if (current == -1) {
// Initialization
current = 0;
resetCurrent = true;
} else if (current >= array.length - 1) {
// Base case
return array[array.length - 1];
}
try {
int i = current++;
return Math.max(array[i], largestElement(array));
} finally {
if (resetCurrent) {
current = -1;
}
}
}
throw new IllegalArgumentException("Input array is empty.");
}
If you can create another method, everything would be much simpler.
private static int recursiveFindLargest(int [] array, int i) {
if (i > 0) {
return Math.max(array[i], recursiveFindLargest(array, i-1));
} else {
return array[0];
}
}
public static int largestElement(int [] array) {
// For empty array, we cannot return a value to indicate this situation,
//all integer values are possible for non-empty arrays.
if (array.length == 0) throw new IllegalArgumentException();
return recursiveFindLargest(array, array.length - 1);
}
For this problem you really need to think about working with the base case. Take a look at some of the simple cases you would have to deal with:
If the array is length 1, then you return the only value
If the array is length 2, then you return the maximum of the two values
If the array is length 3, then ?
From the above we can get an idea of the structure of the problem:
if array.length == 1 then
return array[0]
else
return the maximum of the values
In the above if we have only one element, it is the maximum value in the list. If we have two values, then we have to find the maximum of those values. From this, we can then use the idea that if we have three values, we can find the maximum of two of them, then compare the maximum with the third value. Expanding this into pseudo code, we can get something like:
if array.length == 1 then
return array[0]
else
new array = array without the first element (e.g. {1, 2, 3} => {2, 3})
return maximum(array[0], largestElement(new array))
To explain the above a little better, think of execution like a chain (example for {1, 2, 3}).
Array: {1, 2, 3}, maximum(array[0] = 1, largestElement(new array = {2, 3}))
Array: {2, 3}, maximum(array[0] = 2, largestElement(new array = {3}))
Array: {3}, array[0] = 3 => length is 1 so return 3
The above then rolls back up the 'tree' structure where we get:
maximum (1, maximum(2, (return 3)))
Once you have the maximum value, you can use the sample principle as above to find the index with a separate method:
indexOf(array, maximum)
if array[0] == maximum then
return 0
else if array.length == 1 then
return -1
else
new array = array without the first element (e.g. {1, 2, 3} => {2, 3})
result = indexOf(new array, maximum)
return (result == -1) ? result : result + 1
For looking into this more, I would read this from the Racket language. In essence it shows the idea of array made purely from pairs and how you can use recursion to do iteration on it.
If you are interested, Racket is a pretty good resource for understanding recursion. You can check out University of Waterloo tutorial on Racket. It can give you a brief introduction to recursion in an easy to understand way, as well as walking you through some examples to understand it better.
You don't need to keep a largest variable outside your method - that's generally not a good practice with recursion which should return all context of the results.
When you think about recursion try to think in terms of a simple base case where the answer is obvious and then, for all other cases how to break it down into a simpler case.
So in pseduo-code your algorithm should be something like:
func largest(int[] array)
if array has 1 element
return that element
else
return the larger of the first element and the result of calling largest(remaining elements)
You could use Math.max for the 'larger' calculation.
It's unfortunate that you can't change the arguments as it would be easier if you could pass the index to start at or use lists and sublists. But your copying method should work fine (assuming efficiency isn't a concern).
An alternative to the algorithm above is to make an empty array the base case. This has the advantage of coping with empty arrays (by return Integer.MIN_VALUE):
int largest(int[] array) {
return array.length == 0
? Integer.MIN_VALUE
: Math.max(array[0], largest(Arrays.copyOfRange(array, 1, array.length)));
}
Here is working example of code with one method param
public int max(int[] list) {
if (list.length == 2) return Math.max(list[0], list[1]);
int max = max(Arrays.copyOfRange(list, 1, list.length));
return list[0] < max ? max : list[0];
}
private static int maxNumber(int[] arr,int n,int max){
if(n<0){
return max;
}
max = Math.max(arr[n],max);
return maxNumber(arr,n-1,max);
}
I have a program that creates a class dictionary, in which it populates and arrayList of strings with words given from a command line argument(in alphabetical order, all different lengths). Anyway, I need to implement binary search to look for a prefix in the dictionary as part of a backtracking method. I run into problems when the prefix is longer than the word in the dictionary---I tried to adjust binary search for this situation but it is producing incorrect results. I really don't understand binary search enough to fix this issue. If I don't account for the issue of a prefix being longer than a word, it .subString produces string indexoutofbounds. Any help would be greatly appreciated.
public int searchPrefix(String prefixKey){
int minIndex=0;
int maxIndex= newDictionary.size()-1;
return searchPrefix( prefixKey, minIndex,maxIndex);
}
public int searchPrefix(String prefixKey, int minIndex, int maxIndex){
if(minIndex>maxIndex){
return-1;
}
int midIndex=(maxIndex-minIndex)/2+minIndex;
if (prefixKey.length()>newDictionary.get(midIndex).length()){
return searchPrefix( prefixKey, midIndex+1,maxIndex);
}
else if(newDictionary.get(midIndex).length(<prefixKey.length()&&newDictionary.get(midIndex).compareTo(prefixKey.substring(0,newDictionary.get(midIndex).length()))>0){
return searchPrefix(prefixKey,minIndex,maxIndex);
}
else if(newDictionary.get(midIndex).substring(0,prefixKey.length()).compareTo(prefixKey)>0){
return searchPrefix(prefixKey,minIndex,maxIndex-1);
}
else if(newDictionary.get(midIndex).length()<prefixKey.length()&&newDictionary.get(midIndex).compareTo(prefixKey.substring(0,newDictionary.get(midIndex).length()))<0){
return searchPrefix(prefixKey,minIndex,maxIndex);
}
else if(newDictionary.get(midIndex).substring(0,prefixKey.length()).compareTo(prefixKey)<0){
return searchPrefix( prefixKey, midIndex+1,maxIndex);
}
else
return midIndex;
}
Have taken the binarySearch method from Collections class and modified as per your need.
private static int binarySearch(List<String> list, String key) {
int low = 0;
int high = list.size() - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
String midVal = list.get(mid);
int cmp = -1;
if (midVal.length() > key.length())
cmp = midVal.substring(0, key.length()).compareTo(key);
else
cmp = key.substring(0, midVal.length()).compareTo(midVal) * -1;
if (cmp < 0)
low = mid + 1;
else if (cmp > 0)
high = mid - 1;
else
return mid; // key found
}
return -1; // key not found
}
Hope this will help you.
The String method compareTo takes care of String values of different length, so that (e.g.) "ABC" precedes "ABCD" so all of these case distinctions in your method aren't really necessary.
The cascaded of statement begins:
if (prefixKey.length() > newDictionary.get(midIndex).length()){
return searchPrefix( prefixKey, midIndex+1,maxIndex);
}
else if(newDictionary.get(midIndex).length() < prefixKey.length() && ...
But theses conditions are identical, which means that the second branch is never reached.
You have two statements:
return searchPrefix(prefixKey,minIndex,maxIndex);
Under no circumstances should a recursive call be made with exactly the same parameters as were passed to the current call: infinite recursion results.
Why can't you use Arrays.binarySearch?
I can't really suggest an improvement because you haven't described the problem. Please provide an example, giving a small dictionary and a set of keys with expected results.
I am a fresh student in computer science and currently we study Java recursion. Unfortunately, the academy only explains the following regarding this topic:
What recursion means.
There are 2 types of cases when using a recursive algorithm: base cases and recursive cases and their purpose.
An example of factorial and Fibonacci implementation using recursion.
Now I got the following exercise:
Two integer numbers will be called "strangers" if their greatest common divisor (aka GTC) is ONLY 1". For example, the numbers 8 and 9 are "strangers" because their GTC is 1. However, 8 and 9 are not "strangers" because their GTC is 2.
Please implement a recursive method which receives an array of integers, and returns "true" if every pair numbers in this array are strangers, and "false" otherwise.
Method signature must be as follows:
public boolean checkGCD(int[] values)
For example:
{3, 5, 7, 11} -> method will returns true.
{4, 7, 8, 9} -> method will returns false because 4 and 8 are not strangers.
For assistance, you can use the following method for finding GTC (Euclidean algorithm):
private static int gcd(int n, int m){
if (m==0)
return n;
return gcd(m,n % m);
}
In addition, the method checkGCD(int[] values) should be overloaded...
Loops cannot be used!
The above can be done very easily with a nested loop, but I must use recursion!
I understand that I need to use an overloaded method which gets the array, lo index and hi index.
So this is what I came up in mind:
######
Base case: if there is at least one pair of numbers in the array which are not strangers, method returns false (no need to continue the comparison...).
######
Comparison will be done in the following way: lo index points to the 1st cell -> hi index points to the 2nd cell -> comparing -> hi index is incremented by 1 until it reaches the last cell of the array.
Then, lo index is incremented by 1, and then repeating the above.
So bottom line, I should compare the first cell to all consecutive cells, compare the 2nd to all consecutive cells, the 3rd etc...
########
If all pairs of numbers are strangers, I need something else to stop recursion. Therefore, if all pairs are strangers, it means that lo index and hi index will eventually point to the last cell (cause both lo and hi index has incremented gradually, and they reach the last array cell after all comparisons turned out to be OK i.e strangers).
The following is the overloaded function:
private static boolean checkGCD(int[] values, int lo, int hi)
{
if ( (gcd(values[lo], values[hi]) )!= 1 )
return false;
else if (lo < values.length-1 && hi < values.length-1)
return checkGCD(values, lo, hi+1);
else if (lo < values.length-2 && hi == values.length-1)
return checkGCD (values, lo+1, lo+2);
if (lo == values.length-1 && hi == values.length-1)
return true;
} -> Compiler says "missing return statement"**
The following is the method the exercise requires to have, and it basically just calls the overloaded method which does everything recursively.
public static boolean checkGCD(int[] values)
{
return checkGCD(values, 0, 1);
}
When I try to compile, I get "missing return statement" which points to the close bracket in the overloaded function
But I do use "return" in the overloaded function.
Please clarify how to fix. I am sure after compilation error, the above overloaded function is still not OK.
You get the compiler error because, if every if fails, the method does not return anything. The solution is add the appropriate return statement when the final if fails.
Not to give the answer away, but here's a strong hint: the base case is an array with two elements. All larger arrays are recursive cases.
There's a general pattern for going through a list with a recursion (pseudocode):
Result f(List f) {
if(f is an empty list) {
return Result for an empty list;
} else {
return (Result for head of list) merged with f(tail of list)
}
}
Since you're using arrays, rather than a type with convenient head() and tail() methods, you could pass in an index to say how much of the array you want to process. When index == array.length you are processing an "empty list".
boolean allMembersPositive(int[] array, int index) {
if(index == array.length) {
return true;
} else {
return (array[index] >=0) && (allMembersPositive(index + 1));
}
}
It's a small step to adapt this to consume two array items per recursive call.
I can guarantee you that when you understand recursion clearly you are going to level up your programming skills.
I recommend reading these URLs:
http://howtoprogramwithjava.com/java-recursion/
http://danzig.jct.ac.il/java_class/recursion.html
Now, lets move back to your questions. I think that is one possible way to implement it:
public class Test {
public static void main(String[] arguments) {
int[] strangers = { 3, 5, 7, 11 };
int[] acquaintances = { 4, 7, 8, 9};
boolean verifyStrangers = checkGCD(strangers);
boolean verifyAcquaintances = checkGCD(acquaintances);
System.out.println(verifyStrangers);
System.out.println(verifyAcquaintances);
}
public static boolean checkGCD(int[] values) {
return checkGCD(values, 0, 1);
}
/*
* I'm really not sure why your professor wants this method signature: "checkGCD(int[] values, int i, int j)"
* I'm coding what I understood from the problem.
*/
private static boolean checkGCD(int[] values, int i, int j) {
boolean result = true;
if (gcd(values[i], values[j]) != 1){
result = false;
}
j++;
if (j < values.length ) {
result = result && checkGCD(values, i, j);
}
return result;
}
private static int gcd(int n, int m) {
if (m == 0)
return n;
return gcd(m, n % m);
}
}
I managed to solve the exercise.
public static int gcd(int n, int m)
{
if (m==0)
return n;
return gcd(m,n % m);
}
private static boolean checkGCD(int[] values, int lo, int hi)
{
// System.out.println("lo is " + lo + " hi is " + hi);
// System.out.println("");
// System.out.println("[lo] is " + values [lo] + " [hi] is " + values[hi]);
// System.out.println("");
if ( (gcd(values[lo], values[hi]) )!= 1 )
return false;
if (lo < values.length-1 && hi < values.length-1)
return checkGCD(values, lo, hi+1);
if (lo < values.length-2 && hi == values.length-1)
return checkGCD(values, lo+1, lo+2);
if (lo == values.length-2 && hi == values.length-1)
return true;
return true;
}
public static boolean checkGCD(int[] values)
{
return checkGCD(values, 0, 1);
}
:-)
Hey there I'm using a binary search method to find a number in an array, and I'm getting a StackOverflow error when looking for a number other than the one in the middle.
Here is my code:
public static <T extends Comparable< ? super T>>
int find(T [] a, T x, int low, int high){
if(low>high)
throw new IllegalArgumentException();
int tmp = (high-low)/2;
if(a[tmp].compareTo(x)==0)
return tmp;
else if(a[tmp].compareTo(x)>0)
find(a,x,tmp,high);
else if(a[tmp].compareTo(x)<0)
find(a,x,low,tmp);
return -1;
}
Also, if I try to look for a number under tmp, it returns -1.
I feel like I'm missing something but can't figure out what.
Thanks in advanced!
This is the problem:
if(a[tmp].compareTo(x)>0)
find(a,x,tmp,high);
else if(a[tmp].compareTo(x)<0)
find(a,x,low,tmp);
You should be using tmp + 1 in the first case and tmp - 1 in the second. Otherwise if you've got (say) low = 0, high = 1 then you'll potentially end up perpetually calling with the same arguments; tmp will end up being 0, and if x is more than a[0] you'll just call find(a, x, 0, 1) again.
That's my gut feeling, anyway. You should really log what happens in terms of the low/high values being used - I'm sure you'll see some sort of repetition before it croaks.
EDIT: You've also got the comparison round the wrong way. a[tmp].compareTo(x) will return a value less than 0 if a[tmp] is less than x - i.e. you ought to look later in the array, not earlier.
EDIT: Currently your "exit with -1" code is broken - you'll only ever return -1 if a[tmp].compareTo(x) returns a non-zero value which is neither above zero nor below zero. I challenge you to find such an integer :) (It would also do it if compareTo were unstable, but that's a separate issue...)
One option is to detect if high == low - at that point, if you haven't hit the right value, you can return -1:
int comparison = a[tmp].compareTo(x); // Let's just compare once...
if (comparison == 0) {
return tmp;
}
// This was our last chance!
if (high == low) {
return -1;
}
// If a[tmp] was lower than x, look later in the array. If it was higher than
// x, look earlier in the array.
return comparison < 0 ? find(a, x, tmp + 1, high) : find(a, x, low, tmp - 1);
You need:
else if(a[tmp].compareTo(x)>0)
find(a,x,tmp - 1,high);
else if(a[tmp].compareTo(x)<0)
find(a,x,low,tmp + 1);
Otherwise you'll basically end up recursively calling the function with the same arguments when you have low = 0 and high = 1 for example. The thing is that you don't really need to look at the element at mid again because you already know if it is greater or smaller in relation to the value you are trying to find.
Also, instead of the IllegalArgumentException, you should return -1 if high is lesser than low.
public static <T extends Comparable< ? super T>>
int find(T [] a, T x, int low, int high){
if(low>high)
throw new IllegalArgumentException();
int tmp = (high+low)/2;//replaced - with + for average
if(a[tmp].compareTo(x)==0)
return tmp;
else if(a[tmp].compareTo(x)>0)
return find(a,x,tmp,high); //return the found index
else if(a[tmp].compareTo(x)<0)
return find(a,x,low,tmp);
return -1;
}
if tmp is the average of high and low you need to add them and then divide by 2
also return the found value on the recursive call