Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 26

Thread: Binary Search

  1. #11
    Join Date
    Sep 2007
    Location
    Victoria, BC Canada
    Posts
    185
    Another thought ... Trim.

    If either of the strings has a blank space at the beginning or end ... they won't match
    "Joe Blow" <> "Joe Blow " <> " Joe Blow" even if the case does:

    LowerCase(Trim(searcharray[Mid].Username)) < LowerCase(Trim(EDTSearchUser.Text))

  2. #12
    Join Date
    Jun 2007
    Posts
    47
    All the usernames are saved as lowercase so case is not an issue. Why do you need to see anymore code or contents of the file? Im positive its just the logic of the algorithm.

    Will add in Trim, no an issues but could be thanks for bring to my attention.

  3. #13
    Join Date
    Sep 2007
    Location
    Victoria, BC Canada
    Posts
    185
    Quote Originally Posted by $eymour View Post
    All the usernames are saved as lowercase so case is not an issue.
    Do searches for all except the last record work?

  4. #14
    Join Date
    Jun 2007
    Posts
    47
    Yes, all searchs work fine just if you search for the last record its not found!

  5. #15
    Join Date
    Sep 2007
    Location
    Victoria, BC Canada
    Posts
    185
    Have you tried [knowing you're searching for the last record] putting a breakpoint on:
    Top := Mid - 1
    and if it breaks there ... check your two string values.

  6. #16
    Join Date
    Nov 2006
    Posts
    233
    Quote Originally Posted by TDelphiHobbyist View Post
    I wouldn't rely on < or > alone to compare strings, specifically where there is a chance that case may change [LowerCase < UpperCase], try:

    LowerCase(searcharray[Mid].Username) < LowerCase(EDTSearchUser.Text)
    unless you know you are matching case exactly ...

    A thought ...
    Code:
    if StrComp(SearchArray[Mid].Username, EDTSearchUser.Text) < 0 then
    Would be even better
    Last edited by Blueaura; 11-25-2007 at 01:36 PM.
    Guide to Learning Delphi - A list of resources and tips. If you have a question read this.
    Delphi Fusion Delphi Articles and Forum
    My Blog - Random Delphi related ramblings by a hobby programmer.
    Delphi Fusion Team - Join my SETI@home and ClimatePrediction.Net Team

    You see what power is - holding someone else's fear in your hand and showing it to them!

  7. #17
    Join Date
    Sep 2007
    Location
    Victoria, BC Canada
    Posts
    185
    Quote Originally Posted by Blueaura View Post
    Code:
    if StrComp(SearchArray[Mid].Username, EDTSearchUser.Text) = 0 then
    Would be even better
    Yea ... was going to suggest this but it's a case sensitive comparator and I thought there would be a chance that the TextToFind could have been in LowerCase while the TextStrored in ProperCase.

    Not the case <grin> apparently.

    Edit:
    Sorry ... was thinking about CompareStr, have to look up StrComp although CompareText will ignore case - so I've found.
    Last edited by TDelphiHobbyist; 11-25-2007 at 01:46 PM.

  8. #18
    Join Date
    Nov 2006
    Posts
    233
    Well, I've done my old fashioned debugging with a pen and paper routine, and the conclusion is that if anything is wrong it is your string logic (use StrComp) or what I suggested previously about the variable Top being one too few.

    Step through the iteration with a small SearchArray, you can manually edit it in the debugger if you have too. Check that each variable has a value which you would expect and look for any unexpected jumps out of the loop.

    That's all the best advice I can think of.
    Guide to Learning Delphi - A list of resources and tips. If you have a question read this.
    Delphi Fusion Delphi Articles and Forum
    My Blog - Random Delphi related ramblings by a hobby programmer.
    Delphi Fusion Team - Join my SETI@home and ClimatePrediction.Net Team

    You see what power is - holding someone else's fear in your hand and showing it to them!

  9. #19
    Join Date
    Sep 2007
    Location
    Victoria, BC Canada
    Posts
    185
    Me too:

    Code:
    Last three records are 8, 9 and 10
    bot = 8
    top = 10
    mid = 18 div 2 = 9
    a < b
    bot = inc(mid) = 10 
    top = 10
    mid = 20 div 2 = 10
    
    
    Last three records are 7, 8 and 9
    bot = 7
    top = 9
    mid = 16 div 2 = 8
    a < b
    bot = inc(mid) = 9
    top = 9
    mid = 18 div 2 = 9
    of course it could be flawed

    I thought it was a DIV problem.

  10. #20
    Join Date
    Jun 2007
    Posts
    47
    There are six accounts

    1 admin
    2 ben
    3 coach
    4 dean
    5 glen
    6 player

    Ok steped throught and found..

    When mid = 1 admin is found as expected
    mid = 3 ben is found
    mid = 4 coach is found
    mid = 5 dean is found
    mid = 6 glen is found

    I check and Top is 6, when make it 7 still does not find last record.

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

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