VB.NET - Implimentation of Binary Search.

Discuss whatever you want here--both QB and non-QB related. Anything from the DEF INT command to the meaning of life!

Moderators: Pete, Mods

Post Reply
Anonymous

VB.NET - Implimentation of Binary Search.

Post 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
Digital Shadow
Coder
Posts: 23
Joined: Sun Oct 03, 2004 10:26 pm
Location: Prince George, BC, Canada

Post by Digital Shadow »

what is this madness?!?!? VB in this MSDOS-Sanctuary, go back to your roots!

good stuff tho... :wink:
People laugh at me because I'm different, I laugh because there all the same... Soooo, if were all laughing, I fail to see the problem
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Traitor!
Blasphemy!

Get him!



Tar and feathers, TAR and FEATHERS!





Nice code.. :D
I have left this dump.
Anonymous

You guys are funny.

Post 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
Post Reply