Results 1 to 8 of 8

Thread: Binary search

  1. #1
    thirdage is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Binary search

    Hello :)

    I'm trying to make an interactive program (guess the number)
    the idea is that i put a number in my head and the program tries to guess it.
    the program should ask whether the number is less or greater than the middle of the search interval.
    the number should be between 1 & 1000.
    so what should the psuedo code be ?

    Thanks!!
    thirdage.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Binary search

    Quote Originally Posted by thirdage View Post
    I'm trying to make an interactive program (guess the number)
    the idea is that i put a number in my head and the program tries to guess it.
    the program should ask whether the number is less or greater than the middle of the search interval.
    the number should be between 1 & 1000.
    Nice assignment!

    so what should the psuedo code be ?
    You first!

    Seriously though, if you don't give it a go first, you'll be cheating yourself out of a tremendous learning opportunity.

    Much luck!

  3. #3
    thirdage is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: Binary search

    I'am just not getting it !!
    Java Code:
     
    
    public class guessNumber 
    {
    	public static void main(String[] args)
    	{
    		System.out.print("Is your number between 1 and 1000 ?");
    		int start=1;
    		int end=1000;
    		int middle =(((end-start)/2)+start);
    		int oldmiddle = 0;
    		boolean numfound = false;
    		while (!numfound)
    		{
    			boolean usersAnswer = StdIn.readBoolean();
    			if (usersAnswer ==true)
    			{
    				ifTrue(middle,start);
    				oldmiddle = (((end -middle)/2)+middle);
    				middle = (((middle-start)/2)+start);
    			}
    			else
    			{
    				ifFalse(oldmiddle,start);
    			}
    		}
    
    
    	}
    	public static void ifTrue(int mid,int start)
    	{
    		
    		System.out.print("is your number less than "+mid+" ?");
    		
    		
    	}
    	public static void ifFalse(int mid,int start)
    	{
    		System.out.print("is your number less than "+mid+" ?");
    	}
    
    }
    I tried this ... but i can't configure how to set the interval each time!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: Binary search

    In a range lo ... hi, you pick an element in the middle: lo ... mid ... hi; if the guess is correct, you're done; otherwise if your guess is too high, pick the interval lo ... mid-1, otherwise pick the interval mid+1 ... hi.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    thirdage is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: Binary search

    example :

    am picking 40
    the program asks if it's between 1 & 1000
    my answer: true
    between 1 & 500
    : true
    1&250
    :true
    //
    //
    //
    1&32
    : false
    32 & 63
    :true
    then 47 & 63
    :false

    I mean each time the interval gets small ... and we ask whether it's between the midpoint of interval and end if false
    or start and midpoint if true ... hope you get what I mean.
    Thanks again!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: Binary search

    Quote Originally Posted by thirdage View Post
    hope you get what I mean.
    Erm, no; sorry.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    thirdage is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: Binary search

    If true:
    1-1000 becomes [1--500] where we got the midpoint of 1 & 1000.
    if false after [1--500] was true:
    we check on [500--1000]
    then true
    we check on [750-1000] .... (where 750 = ((1000-500)/2) +500).

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: Binary search

    Have you read my reply #4? The boundaries are important you know ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Problem with binary search
    By Chi in forum New To Java
    Replies: 1
    Last Post: 03-07-2011, 03:23 AM
  2. Binary Search
    By cengho in forum Java Applets
    Replies: 4
    Last Post: 12-24-2010, 10:26 AM
  3. Binary Search Help
    By Plissken in forum New To Java
    Replies: 2
    Last Post: 03-13-2010, 10:34 AM
  4. Binary search tree search method
    By chopo1980 in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 01:42 AM
  5. binary search
    By tranceluv in forum New To Java
    Replies: 10
    Last Post: 01-14-2008, 07:13 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
  •