Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Binary Search Help

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Binary Search Help

    Binary Search Help

    I did the program of Binary Search but got stuck somewhere...Please Help me on this.

    class BinarySearch
        {
        public static void main(String[] args)
        {
        int data[] = {9,11,23,43,54,66,76,78,99};
        int beg = data[0];
        int end = data[data.length-1];
        int mid = (int)((end-beg)/2);
        int item = 78;
        int loc = 0;
        do
        {
        if(item<data[mid])
        {
        end = mid-1;
        }
        else
        beg = mid+1;
        mid = (int)((end-beg)/2);
        }
        while(beg<=end && data[mid]!=item);
     
        if(data[mid]==item)
        System.out.println("Successful");
        else
        System.out.println("Unsuccessful");
        }
        }
    Last edited by OwsumEmam; March 31st, 2011 at 12:49 AM.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Binary Search Help

    1. Post your code in code tags.
    2. Try learning indentation from here.
    3. You are assigning the values to the variables beg, end, data values of array, not actually the index of array.
    4. You should also have to check if your code finds out the specific value or not, if not then???

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Binary Search Help

    Sorry brother, now it's right...!!! but can you help me out of this?

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Binary Search Help

    how can i call the index without loops?

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Binary Search Help

    mid=(end+beg)/2
    not mid=(end-beg)/2;

  6. #6
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Binary Search Help

    But the prob is the same IndexOutOfBoundsException : 54 (first it was on 45)

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Binary Search Help

    You set beg and end to values in the array not the first and last index of the array.

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Binary Search Help

    Why are you doing this?
    int beg=data[0];// wrong actually you are assigning value here but you should assign index.
    //So do,
    int beg=0;
    int end=data.length()-1;

Similar Threads

  1. Binary Search Help!
    By Allicat in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 15th, 2011, 05:03 PM
  2. Binary Search Tree
    By lex25288 in forum Algorithms & Recursion
    Replies: 3
    Last Post: January 19th, 2011, 09:10 AM
  3. Binary Search Loop
    By Tarakara in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 29th, 2010, 09:22 AM
  4. Binary Search Tree
    By Koren3 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 12th, 2009, 09:27 AM
  5. Binary Search Tree implementation
    By Ceasar in forum Java Theory & Questions
    Replies: 3
    Last Post: October 9th, 2009, 01:23 AM

Tags for this Thread