Binary Search

Discussion in 'C Programming' started by willamette3597@gmail.com, Oct 30, 2005.

  1. Guest

    Could Someone tell me Where can I get The result though the Binary
    Search
    eg.
    max , min , mid ,want;
    While ( max > min )
    {
    mid = (min + max )/2 ;
    if ( mid > want ) max = mid + 1;
    else min = mid -1 ;
    Line1 :
    }
    Line2 :
    Where can I get the correct result ? Line1 or Line2 ?
     
    , Oct 30, 2005
    #1
    1. Advertising

  2. Sandeep Guest

    >>Could Someone tell me Where can I get The result though the Binary
    >>Search
    >>eg.
    >>max , min , mid ,want;
    >>While ( max > min )
    >>{
    >>mid = (min + max )/2 ;
    >>if ( mid > want ) max = mid + 1;
    >>else min = mid -1 ;
    >>Line1 :
    >>}


    >>Line2 :
    >>Where can I get the correct result ? Line1 or Line2 ?


    The best way to answer these questions is to take a sample ( an array
    and a key in this case ), and then run through your algorithm/code for
    both positive and negative cases.
     
    Sandeep, Oct 30, 2005
    #2
    1. Advertising

  3. Norm Mann Guest

    "Sandeep" <> wrote in message
    news:...
    >>>Could Someone tell me Where can I get The result though the Binary
    >>>Search
    >>>eg.
    >>>max , min , mid ,want;
    >>>While ( max > min )
    >>>{
    >>>mid = (min + max )/2 ;
    >>>if ( mid > want ) max = mid + 1;
    >>>else min = mid -1 ;
    >>>Line1 :
    >>>}

    >
    >>>Line2 :
    >>>Where can I get the correct result ? Line1 or Line2 ?

    >
    > The best way to answer these questions is to take a sample ( an array
    > and a key in this case ), and then run through your algorithm/code for
    > both positive and negative cases.
    >


    I agree the best way is to run through the program and see how it behaves,
    but I also think one needs to understand the principle of the algorithm one
    is trying to implement.
    There are actually two "correct" results: "match" and "no match".
    "Match" is where "DATA(mid) = want" and usually results in exiting from the
    while loop before the default termination. ("max", "mid" and "min" are
    usually pointers or indexes.)
    "No match" is where the while loop terminates when "max" <= "min".
    This sample routine doesn't even check for a match and if one looks at the
    way "min" and "max" are updated, it may result in an endless loop as it
    narrows the search to a few data entries. If it were working correctly,
    "mid" would be a place to insert "want", if no match was found or it points
    to where "want" is, if a match is found.

    HTH,

    -NM
     
    Norm Mann, Oct 31, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    1
    Views:
    376
    Jack Klein
    Nov 25, 2003
  2. Timmy
    Replies:
    5
    Views:
    494
  3. Replies:
    9
    Views:
    467
    Keith Thompson
    Jul 3, 2009
  4. sanborne
    Replies:
    5
    Views:
    2,070
  5. Bogdan

    Binary tree search vs Binary search

    Bogdan, Oct 18, 2010, in forum: C Programming
    Replies:
    22
    Views:
    3,239
    Michael Angelo Ravera
    Oct 21, 2010
Loading...

Share This Page