Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: Binary Search

  1. #1
    Join Date
    Jun 2007
    Posts
    47

    Binary Search

    I tried and fail to work out why my algorithm wont find the last record in the array

    Code:
    var
      ItemFound, SearchFailed : boolean;
      Top, Bot, Mid : integer;
    
    begin
      ItemFound := false;
      SearchFailed := false;
      Top := arraysize;
      Bot := 1;
      repeat
        Mid := (Top + Bot) DIV 2;
        if searcharray[Mid].Username = EDTSearchUser.Text then
        begin
          ItemFound := true;
        end
        else
        begin
          if Bot > Top then
          begin
            SearchFailed := true;
          end
          else
          begin
            if searcharray[Mid].Username < EDTSearchUser.Text then
            begin
              Bot := Mid + 1;
            end
            else
            begin
              Top := Mid -1;
            end;
          end;
        end;
      until (ItemFound = true) or (SearchFailed = true);
      if ItemFound = true then
         begin
          EDTViewUsername.Text := searcharray[mid].Username;
          EDTViewpassword.Text := searcharray[mid].Password;
          EDTViewLevel.Text := searcharray[mid].Level;
          EDTviewemail.Text := searcharray[mid].email;
         end
      else
        ShowMessage('Not found');

  2. #2
    Join Date
    Sep 2007
    Location
    Victoria, BC Canada
    Posts
    185
    Quote Originally Posted by $eymour View Post
    I tried and fail to work out why my algorithm wont find the last record in the array

    [code]var
    ItemFound, SearchFailed : boolean;
    Top, Bot, Mid : integer;
    Although I haven't studdied your code in detail and I'm kinda winging it .. you "may" be assigning the wrong values to your variables
    Use:
    Low(ArrayName) to get the index of the first record and
    High(ArrayName) to get the index of the last record.

    Length(ArrayName) will give you the number of records [a 1 based value].

    Unless you've specified otherwise Arrays are 0 based IOW Bot <> 1 so there should be an Array[0] you may be missing out on.

    There may be something in this you can work with until the gurus arrive

  3. #3
    Join Date
    Jun 2007
    Posts
    47
    Thanks but the array is filled with records from disk, starts at 1 not 0 this is specified in the global varibles searcharray:array[1..10] of Tlogin; i have used arraysize := filesize(loginfile); insted of Length(ArrayName).

    The search works just not for the last record.. i appreciate the help but cant start changing things to other peoples style of code as this is an a level project and my teacher will spot that its not my style..

  4. #4
    Join Date
    Nov 2006
    Posts
    233
    Try
    Code:
    Top := ArraySize + 1;
    Should solve your problem, as to exactly why... not sure but it is more than likely because FileSize(LoginFile) returns a size from 0..X so X - 1, now you are using an array 1..X so you need to use X + 1. Capisce?

    If it still doesn't solve it try the old fashioned debugging method of making a table of each variable and work through the code manually noting any changes in the variable for each iteration. Old fashioned but effective.
    Last edited by Blueaura; 11-25-2007 at 12:13 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!

  5. #5
    Join Date
    Jun 2007
    Posts
    47
    yeah i understand.. but changed it, no effect. Still have the same problem!

  6. #6
    Join Date
    Sep 2006
    Posts
    18
    It wont find only last record?
    Anyway, try to change
    searcharray[Mid].Username < EDTSearchUser.Text
    to
    searcharray[Mid].Username > EDTSearchUser.Text
    Giggity

  7. #7
    Join Date
    Jun 2007
    Posts
    47
    Yes its only the last record.. and if i was to change < to > then it wouldnt find anything!

  8. #8
    Join Date
    Sep 2006
    Posts
    18
    Can you post whole source code? Cause from the part that you posted everything seems to be correct...
    Giggity

  9. #9
    Join Date
    Sep 2007
    Location
    Victoria, BC Canada
    Posts
    185
    Quote Originally Posted by $eymour View Post
    Yes its only the last record.. and if i was to change < to > then it wouldnt find anything!
    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 ...

  10. #10
    Join Date
    Sep 2006
    Posts
    18
    Good thought! $eymour, maybe you can post the contents of the file you're using?
    Giggity

Page 1 of 3 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
  •