29. august 2001 - 05:08Der er
5 kommentarer og 1 løsning
Binary search routine...
Jeg har flg. beskrivelse af en \"binær\" søgerutine fra en engelsk newsgroup, men er der en der kan lave koden til den ?
Et array med f.eks. 1000 elementer er fyldt med strenge af variabel længde, og derefter sorteret alfabetisk. Her kommer søgetingen så ind og det er DEN jeg gerne vil have lavet:
\"A better way is the \"binary\" search. You keep the array sorted into alphabetical order. Look at the middle reoord. If it is the record you want, exit. If the middle record _follows_ the record you want in alpha order, mark the middle as the new \'top\' position, recompute the middle position and try again. If the middle record _precedes_ the record you are searching for, mark the middle position as the new \'bottom\' and repeat. If you get the details right, this is guaranteed to find the search record (or prove that it\'s not there) in a 256-element array in just 8 tries. The work factor is a logarithmic function of the array size, so a million - element array would need slightly less than 20 tries.\"
Der bliver investeret massivt i AI. Teknologien er mere tilgængelig end nogensinde, og ambitionerne er høje. Alligevel oplever mange virksomheder, at resultaterne udebliver.
Public Function binSearch(strTest As String, minPos As Integer, maxPos As Integer) As String Dim midPos As Integer midPos = ((maxPos - minPos) / 2) + minPos Debug.Print minPos, midPos, maxPos If testArray(midPos) = strTest Then binSearch = midPos ElseIf minPos = maxPos Then binSearch = \"Null\" ElseIf testArray(midPos) < strTest Then binSearch = binSearch(strTest, midPos, maxPos) Else binSearch = binSearch(strTest, minPos, midPos) End If End Function
Private Sub Command1_Click() MsgBox binSearch(Text1, 0, 10) End Sub
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.