Page 1 of 1

VB.NET - Implimentation of Binary Search.

Posted: Mon Aug 02, 2004 3:46 pm
by Anonymous

Code: Select all

Option Explicit On 

Module Main

  Public Sub Main()

  Dim ssource(5) As String

    ssource(0) = "alpha"
    ssource(1) = "beta"
    ssource(2) = "cow"
    ssource(3) = "monster"
    ssource(4) = "monsters go"
    ssource(5) = "zzzz"

    Debug.WriteLine(search(ssource, "monsters go"))

  End Sub

  Public Function search(ByVal ssource() As String, ByVal starget As String) As Long

  Dim lhigh As Long = UBound(ssource)
  Dim llow As Long = -1
  Dim lprobe As Long

    While (lhigh - llow > 1)

      lprobe = (lhigh + llow) \ 2
        If (ssource(lprobe) < starget) Then
           llow = lprobe
        Else
           lhigh = lprobe
        End If

    End While

    If (lhigh = UBound(ssource) Or ssource(lhigh) <> starget) Then
       Return -1
    Else
       Return lhigh
    End If

  End Function

  Public Function search(ByVal lsource() As Long, ByVal ltarget As Long) As Long

  Dim lhigh As Long = UBound(lsource)
  Dim llow As Long = -1
  Dim lprobe As Long

    While (lhigh - llow > 1)

      lprobe = (lhigh + llow) \ 2
        If (lsource(lprobe) < ltarget) Then
           llow = lprobe
        Else
           lhigh = lprobe
        End If

    End While

    If (lhigh = UBound(lsource) Or lsource(lhigh) <> ltarget) Then
       Return -1
    Else
       Return lhigh
    End If

  End Function

End Module
-Jeff

Posted: Wed Oct 13, 2004 2:17 pm
by Digital Shadow
what is this madness?!?!? VB in this MSDOS-Sanctuary, go back to your roots!

good stuff tho... :wink:

Posted: Wed Oct 13, 2004 5:03 pm
by Z!re
Traitor!
Blasphemy!

Get him!



Tar and feathers, TAR and FEATHERS!





Nice code.. :D

You guys are funny.

Posted: Tue Oct 19, 2004 6:34 pm
by Anonymous
I code in VB.NET (this is what I do for work btw) and I share it with the QB Community so that every one who wants to can learn about vb.net.

I don't do this to damage the community its simply posted here to help.

Besides if I ever get JBASIC going again the syntax will be somewhat a mix of QB45/QB71/VB6/VB.NET... So get used to it.

-Jeff

http://jqb45.crawfordfamily.cc