7 Replies - 389 Views - Last Post: 31 March 2011 - 06:05 AM Rate Topic: -----

#1 OwsumEmam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-March 11

Binary Search Error

Posted 29 March 2011 - 12:05 AM

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");
    }
    }


Is This A Good Question/Topic? 0
  • +

Replies To: Binary Search Error

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1986
  • View blog
  • Posts: 4,839
  • Joined: 10-September 10

Re: Binary Search Error

Posted 29 March 2011 - 02:12 AM

It's always helpful to describe what's going wrong, include error messages, etc.

Just looking at your code:

    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;
        }



You set up 3 items, beg, end, and mid. beg is the first value of the data set, end is the last value of the data set, and mid is the median of beg and end. In the case of the first run, mid = 45. Then, in your do loop, compare your target item to data[mid], or data[45]. You should be getting an error, which you should have included.

Rethink what you're comparing in your do loop.
Was This Post Helpful? 0
  • +
  • -

#3 OwsumEmam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-March 11

Re: Binary Search Error

Posted 30 March 2011 - 09:44 PM

Yes bro, I want to compare the middle term with the mid index value. If greater then set beg = mid+1; else end = mid-1 and then again get the mid value and compare if it is found in any of the index if not in array then show Unsuccessful.
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Binary Search Error

Posted 30 March 2011 - 10:37 PM

Think again.
You have end = 99.
You have beg = 9.
So you calculate mid = 45.
Then on line 13 you say
if(item<data[mid])
Now, mid is 45, so data[mid] is ??? That's data[45], the 46th element of your 9-element array.

Does that look like a problem now?

This post has been edited by r.stiltskin: 30 March 2011 - 10:38 PM

Was This Post Helpful? 0
  • +
  • -

#5 OwsumEmam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-March 11

Re: Binary Search Error

Posted 30 March 2011 - 10:51 PM

View Postr.stiltskin, on 30 March 2011 - 10:37 PM, said:

Think again.
You have end = 99.
You have beg = 9.
So you calculate mid = 45.
Then on line 13 you say
if(item<data[mid])
Now, mid is 45, so data[mid] is ??? That's data[45], the 46th element of your 9-element array.

Does that look like a problem now?



I know the problem of this program but this is what I want to know. How to call the index without loops.
This is definitely the problem as you just said in the "mid" but how to call the index in java? that the Q.
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Binary Search Error

Posted 30 March 2011 - 11:14 PM

That is the correct syntax for an array index. The problem is just the value that you are putting there. data[mid] is fine as long as mid is no more than 8 and no less than 0.

Read the algorithm again. beg and end are supposed to be locations, not data values.
Was This Post Helpful? 0
  • +
  • -

#7 OwsumEmam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-March 11

Re: Binary Search Error

Posted 30 March 2011 - 11:29 PM

View Postr.stiltskin, on 30 March 2011 - 11:14 PM, said:

That is the correct syntax for an array index. The problem is just the value that you are putting there. data[mid] is fine as long as mid is no more than 8 and no less than 0.

Read the algorithm again. beg and end are supposed to be locations, not data values.



Brother kindly tell me how to make it location not the values. I know no way but loops but I can't use it here.
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Binary Search Error

Posted 31 March 2011 - 06:05 AM

Location means the index in the array, not the value of the element stored there.

Initially, beg should be 0 and end should be the highest index in the array -- in this case 8.

This post has been edited by r.stiltskin: 31 March 2011 - 06:05 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1