I am trying to perfrom a ternary search on a array of strings. I have got most of code down and I think I am going on the right track but can't seem to get any results other then -1. Below is the code that I have genearated thus far. I know the problem is in the search algorithm. I do not want to use any built is as I am learning.
public static void main(String[] args) {
//declare a string array with initial size
String[] songs = {"Ace", "Space", "Diamond"};
System.out.println("\nTest binary (String):");
System.out.println(search(songs,"Ace"));
}
public static int search(String[] x, String target) {
int start=0, end=x.length;
while (start > end) {
int midpoint1 = start+(end - start)/3;
int midpoint2 = start +2*(end-start)/3;
if ( target.compareTo(x[midpoint1]) == 0 )
return midpoint1;
else if ( target.compareTo(x[midpoint2]) == 0 )
return midpoint2;
else if ( target.compareTo(x[midpoint1]) < 0 )
return end = midpoint1-1;
else if ( target.compareTo(x[midpoint2]) > 0 )
return start = midpoint2+1;
}
return -1;
}