Recent

Author Topic: Naive search and binary search don't give the same result  (Read 3455 times)

Sibylla

  • New Member
  • *
  • Posts: 13
Naive search and binary search don't give the same result
« on: October 07, 2013, 06:32:03 pm »
I did implement both:
- Naive search
- binary search

in Lazarus, but they yield diffrent results. The naive search performs the search successfully (albeit slowly...), but the binary search fails to search all elements.
Could someone diagnose the cause of the problem in the binary search. The source code can be found at:

https://github.com/AdminSibylla/LazarusBinarySearch

Thanks in advance.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Naive search and binary search don't give the same result
« Reply #1 on: October 07, 2013, 07:17:50 pm »
remove all the slpivot references from the binary search, use the functions parameter Strings to compare with the subStr (which it must be renamed to a searchStr or something). in slPivot you do not have enough items to use it for comparison.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Sibylla

  • New Member
  • *
  • Posts: 13
Re: Naive search and binary search don't give the same result
« Reply #2 on: October 07, 2013, 09:44:41 pm »
I did as requested. Naive search does not use the > operator and only uses the = operator. So presumably the problem was with the > operator. By replacing it byWideCompareText and handling ITF8 as follows:

else if WideCompareText(UTF8Decode(Strings[iPivot]), UTF8Decode(SubStr)) > 0 then

instead of:

else if (Strings[iPivot] > SubStr) then

It works! And now both algorithms yield 326/375 words.

Many thanks for your help.

 

TinyPortal © 2005-2018