Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Argument starter - The roots of all evil
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00938079
Message ID:
00940903
Vues:
55
>>Thanks, Tamar
>>
>>That's exactly as I learnt it and understood it (if you read my question then I roughly described it thus). OK I was interested in how George would do that in a do while loop.
>>
>>Terry
>>
>>>>... As I learnt it in college it means going backward and foreward in the list, halving the distance as you go (in essence). If that's so, please could you indicate how does ascan() do it, and how you would do it via a do while?
>>>>
>Terry,
>
>Here's an example. I'm doing this off the top of my head, so it hasn't been debugged. Nevertheless, it should be close enough. If there are questions (or mistakes) please let me know. This function will return either the offset into a single dimension array, or the next greatest value. Naturally, the array is passed by referene
FUNCTION BArraySearch
>
>LPARAMETERS tarray, tsearchfor, tlsoftsearch
>
>LOCAL lnfirst, lnlast, lnmid, llfound, lnresult
>lnfirst = 1
>lnlast = ALEN(tarray, 1)
>llfound = .F.
>lnresult = 0
>DO WHILE NOT llfound AND lnfirst < lnlast
>  lnmid = INT(lnfirst + lnlast)
>  llfound = (tsearchfor = tarray[lnmid])
>  IF NOT llfound THEN
>    IF tsearchfor > tarray[lnmid] THEN
>      lnfirst = lnmid + 1
>    ELSE
>      lnlast = lnmid - 1
>    ENDIF
>  ENDIF
>ENDDO
>IF llfound THEN
>  lnresult = lnmid
>ELSE
>  IF llsoftsearch THEN
>    lnresult = lnlast
>  ENDIF
>ENDIF
>RETURN lnresult
The beauty of this is that it can be implemented, not only with a array, but an ordered list or cursor.

George, are the following assumptions correct?
1. You assume that the list is ordered.
2. ASCAN() is not available.
3. lnmid = INT(lnfirst + lnlast) should actually be lnmid = INT(lnlast/2) for the first time around the loop and then add or subtract half the remainder for subsequent iterations.
4. If you start at the mid point and decrement then you are either going to loop forever or get a subscript out of bounds error when it gets to 0.

Here is my attempt:
********************************************************************
Function fSearch( aToSearch, cLookForItem, lFindNearest ) As Integer
********************************************************************
   Local iHalfLength, iItemToTest, iLastLoops As Integer
   iHalfLength = Int( ALen( aToSearch ) / 2 )
   iItemToTest = iHalfLength
   iLastLoops = 0

   Do While cLookForItem # aToSearch[ iItemToTest ] ;
         And iLastLoops < 3

      iHalfLength = Int( iHalfLength / 2 )
      If iHalfLength = 0 Then
         iLastLoops = iLastLoops + 1
         iHalfLength = 1
      Endif
      If cLookForItem < aToSearch[ iItemToTest ] Then
         iItemToTest = iItemToTest - iHalfLength
      Else
         iItemToTest = iItemToTest + iHalfLength
      Endif
   EndDo
   Return Iif( Not lFindNearest And cLookForItem # aToSearch[ iItemToTest ] ;
            , -1 ;
            , iItemToTest )
   EndFunc
censored.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform