Results 1 to 6 of 6

Thread: Search

  1. #1
    Gilgamesh is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default Search

    I have a string vector having strings in an alphabetical order. I use contains() to check if it contains specific strings. Obviously this search is linear, how can I convert it into string binary search?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    how can I convert it into string binary search
    You can write a contains or search method in the class that is working with the Vector, or subclass Vector and: add a method to do binary search or override the contains method or do both, calling the binary search method from contains. You can set it up any way you like when doing this.
    Java Code:
    class Pseudo {
        Vector vector;
    
        /** instance method */
        String contains(String target) {
            // do binary search for target in vector
        }
    }
    class MyVector extends Vector {
        /** override contains method */
        public boolean contains(Object elem) {
            // do your binary search here
            // or call a local method below
            //    to do the binary search
        }
    
        /** with or without overriding contains */
        private_or_public boolean binaryContains(Type arg) {
            // binary search for arg
        }
    }

  3. #3
    Gilgamesh is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    int binarySearch (String word, Vector <String> AlphabetizedListVector){
    int lefthand=0;
    int righthand=AlphabetizedListVector.size() - 1;
    while (lefthand <= righthand){
    int mid =( lefthand + righthand ) / 2;
    int cmp = word.compareToIgnoreCase(AlphabetizedListVector.ge t(mid));
    if (cmp == 0) return mid;
    if (cmp < 0){
    righthand = mid-1;
    }else{
    lefthand = mid+1;
    }
    }
    return -1;
    // System.out.println("The word "+word+" does not exist.");
    }

    a) am i making a mistake on this?
    Java Code:
    (String word, Vector <String> AlphabetizedListVector)
    ? it says syntax errors on tokens regarding the parentheses and the comma, ';' expected.

    b) how can I instead of 'return -1', print out that the word does not appear in the Vector? thanx

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I tried your code and, after fixing the typo ("ge t") it works okay. The compile errors must be from something else. The error usually gives the line number.
    Java Code:
        public static void main(String[] args) {
            String s = "hello world we are here";
            String[] strs = s.split("\\s");
            Vector<String> v = new Vector<String>();
            for(int j = 0; j < strs.length; j++) {
                v.add(strs[j]);
            }
            int n = binarySearch("we", v);
            System.out.println("n = " + n);
        }
    
        static int binarySearch (String word, Vector <String> AlphabetizedListVector){ 
            int lefthand=0;
            int righthand=AlphabetizedListVector.size() - 1;
            while (lefthand <= righthand){
                int mid =( lefthand + righthand ) / 2;
                int cmp = word.compareToIgnoreCase(AlphabetizedListVector.get(mid));
                if (cmp == 0) return mid;
                if (cmp < 0){
                    righthand = mid-1;
                }else{
                    lefthand = mid+1;
                }
            }
            return -1; 
    //        System.out.println("The word "+word+" does not exist.");
        }

  5. #5
    Gilgamesh is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    a) im sending you the pic to understand me better

    b) how (and where exactly in the .java) can I printout ("The word "+word+" does not exist.") when the word is not found in the Vector?

    1000 thanks
    Attached Thumbnails Attached Thumbnails Search-hardwired11-27.jpg  

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    im sending you the pic to understand me better
    I have no idea what that means, must be some ide thing. Try commenting the entire method out and commenting out the calls to it and see where it takes you.
    where can I printout "The word "+word+" does not exist." when the word is not found in the Vector
    Java Code:
            }
            System.out.println("The word "+word+" does not exist.");
            return -1; 
        }

Similar Threads

  1. How to search with a Comparator
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:39 PM
  2. Search TextFile
    By gsupriyarao@yahoo.com in forum Advanced Java
    Replies: 5
    Last Post: 04-11-2008, 12:03 PM
  3. Search Engine
    By SSam Varghese in forum Java Servlet
    Replies: 5
    Last Post: 01-05-2008, 09:26 AM
  4. Web Search
    By Marcus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-02-2007, 07:51 AM
  5. Search in hibernate
    By Nick15 in forum JDBC
    Replies: 4
    Last Post: 05-09-2007, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •