3

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;
} 
mbomb007
  • 3,788
  • 3
  • 39
  • 68
M Lill
  • 31
  • 2
  • Why do you have your method declared as an int? – ryekayo Apr 20 '15 at 20:18
  • You should learn how to debug these kinds of problems on your own. You'll get stuck with things like this all the time, especially in the beginning. In this case a few prints would've sufficed. – keyser Apr 20 '15 at 20:24

5 Answers5

3

You never get into the loop.

int start=0, end=x.length;
while (start > end)
Thomas
  • 5,074
  • 1
  • 16
  • 12
1

You're while statement is wrong, it should contain start < end. I recomend learning the debug settings that are on most IDEs because if you're going to stick around it makes it so much easier to view the states of vars.

Feek
  • 297
  • 3
  • 15
1

Try this corrected version, additionally to the bug identified by the Thomas and user2789574, you also have a bug in the recursion:

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", 0, 3));

}

public static int search(String[] x, String target, int start, int end) {

    if (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 (x[midpoint1].compareTo(x[midpoint2]) < 0) {
            return search(x, target, midpoint1, end);
        } else {
            return search(x, target, start, midpoint2);
        }
    }

    return -1;
}
xerx593
  • 12,237
  • 5
  • 33
  • 64
  • 1
    Thanks for showing my what I was doing wrong. I have an array larger then the one that I showed that contains 50 strings, I know to change "Ace", 0, 3 to Ace, 0 , 50 but when I run seach another string within the array it just hangs up – M Lill Apr 20 '15 at 20:43
  • "A ternary search algorithm is a technique in computer science for finding the **minimum or maximum** of an **increasing or decreasing** function", (from wikipedia), of course this raises problems on "unsorted" data. – xerx593 Apr 20 '15 at 20:58
  • What you most likely want to implement is not a ternary search, but maybe a [Lookup in a ternary Tree](http://en.wikipedia.org/wiki/Ternary_search_tree#Look_up) ...for this you have to convert your String[] into another structure (or at least ensure a distinct structure/ordder...it is possible to store a k-tree as an array) – xerx593 Apr 20 '15 at 21:04
  • ..this is nor trivial, nor very efficient, the most effecient datastructure for your use case would be the `HashSet`(/`table`)! – xerx593 Apr 20 '15 at 21:25
  • Also the `while` statement makes no sense if you are going to return anyway. It has some overhead on the `if` statement – Sam Segers May 12 '17 at 20:31
0

end = x.length,will always return numeric value greater than zero for not null string and it's comparison with start =0 , it will never enter into the loop.

vipin
  • 45
  • 2
  • 6
0

I am not sure if its still relevant now, but here is the ternary search done with loops in java(assuming that the array is pre-sorted):

  public static int ternSearch(String[] x, String target) {
        
        if((target.toLowerCase()).compareTo(x[0].toLowerCase()) == 0)
        {
            return 0;
        }
        if((target.toLowerCase()).compareTo(x[x.length - 1].toLowerCase()) == 0)
        {
            return x.length-1;
        }
        int mid1 = (int) Math.ceil( x.length/3);
        if((target.toLowerCase()).compareTo(x[mid1].toLowerCase()) == 0)
        {
            return mid1;
        }
        int mid2 = (int) Math.ceil( x.length*2/3);
        if((target.toLowerCase()).compareTo(x[mid2].toLowerCase()) == 0)
        {
            return mid2;
        }
        if((target.toLowerCase()).compareTo(x[0].toLowerCase()) > 0&& (target.toLowerCase()).compareTo(x[mid1].toLowerCase()) < 0)
        {
            for (int i = 1; i < mid1; i++)
            {
                if((target.toLowerCase()).compareTo(x[i].toLowerCase()) == 0) 
                {
                    return i;
                }
            }
        }
        else if((target.toLowerCase()).compareTo(x[mid1].toLowerCase()) > 0&& (target.toLowerCase()).compareTo(x[mid2].toLowerCase()) < 0)
        {
            for (int i = mid1+1; i < mid2; i++)
            {
                if((target.toLowerCase()).compareTo(x[i].toLowerCase()) == 0) 
                {
                    return i;
                }
            }
        }
        else if((target.toLowerCase()).compareTo(x[mid2].toLowerCase()) > 0&& (target.toLowerCase()).compareTo(x[x.length-1].toLowerCase()) < 0)
        {
            for (int i = mid2+1; i < x.length-2; i++)
            {
                if((target.toLowerCase()).compareTo(x[i].toLowerCase()) == 0) 
                {
                    return i;
                }
            }
        }
        return -1;
    }