Array.BinarySearch
Imports System.Collections
Public Class Tester
Shared Sub Main()
Dim integerValues As Integer() = {1, 2, 3, 4, 5, 6}
Dim integerElement As Integer
For Each integerElement In integerValues
Console.WriteLine(integerElement)
Next
Dim result As Integer = Array.BinarySearch(integerValues, 5)
If result >= 0 Then
Console.WriteLine("5 found at element " & result &" in integerValues")
Else
Console.WriteLine("5 not found" & " in integerValues")
End If
End Sub
End Class
1
2
3
4
5
6
5 found at element 4 in integerValues
Array Reverse and Sort
Option Strict On
Imports System
Class Tester
Public Shared Sub DisplayArray(ByVal theArray( ) As Object)
Dim obj As Object
For Each obj In theArray
Console.WriteLine("Value: {0}", obj)
Next obj
Console.WriteLine(ControlChars.Lf)
End Sub
Public Shared Sub Main( )
Dim myArray As [String]( ) = {"W", "s", "J", "t"}
Console.WriteLine("Display myArray...")
DisplayArray(myArray)
Console.WriteLine("Reverse and display myArray...")
Array.Reverse(myArray)
DisplayArray(myArray)
Dim myOtherArray As [String]( ) ={"W", "d", "e", "s", "o", "e", "f", "t"}
Console.WriteLine("Display myOtherArray...")
DisplayArray(myOtherArray)
Console.WriteLine("Sort and display myOtherArray...")
Array.Sort(myOtherArray)
DisplayArray(myOtherArray)
End Sub "Main
End Class "Tester
Display myArray...
Value: W
Value: s
Value: J
Value: t
Reverse and display myArray...
Value: t
Value: J
Value: s
Value: W
Display myOtherArray...
Value: W
Value: d
Value: e
Value: s
Value: o
Value: e
Value: f
Value: t
Sort and display myOtherArray...
Value: d
Value: e
Value: e
Value: f
Value: o
Value: s
Value: t
Value: W
Array.Sort and Array.IndexOf
Imports System.Collections
public class Test
public Shared Sub Main
Dim StringArray() As String = {"This", "is", "a", "test"}
Dim E As IEnumerator = StringArray.GetEnumerator()
While (E.MoveNext())
Console.WriteLine(E.Current())
End While
Dim NewArray(StringArray.Length) As String
Array.Copy(StringArray, NewArray, StringArray.Length)
Console.WriteLine(Array.IndexOf(NewArray, "a"))
End Sub
End class
This
is
a
test
2
Demonstrating binary search of an array
Public Class Tester
Dim Shared array1 As Integer() = New Integer(14) {}
Public Shared Sub Main
Dim i As Integer
For i = 0 To array1.GetUpperBound(0)
array1(i) = 2 * i
Next
Dim searchKey As Integer = 8
Dim element As Integer = BinarySearch(array1, searchKey)
If element <> -1 Then
Console.WriteLine("Found value in element " & element)
Else
Console.WriteLine("Value not found")
End If
End Sub
" performs binary search
Shared Function BinarySearch(ByVal array As Integer(), _
ByVal key As Integer) As Integer
Dim low As Integer = 0 " low index
Dim high As Integer = array.GetUpperBound(0) " high index
Dim middle As Integer " middle index
While low <= high
middle = (low + high) \ 2
If key = array(middle) Then " match
Return middle
ElseIf key < array(middle) Then " search low end
high = middle - 1 " of array
Else
low = middle + 1
End If
End While
Return -1 " search key not found
End Function " BinarySearch
End Class
Found value in element 4
Linear search of an array
Public Class Tester
Public Shared Sub Main
Dim array1 As Integer() = New Integer(19) {}
Dim randomNumber As Random = New Random()
Dim i As Integer
" creates string containing 11 random numbers
For i = 0 To array1.GetUpperBound(0)
array1(i) = randomNumber.Next(1000)
Console.Write ( array1(i) & " ")
Next
Console.WriteLine("")
Dim searchKey As Integer = 12
Dim element As Integer = LinearSearch(searchKey, array1)
If element <> -1 Then
Console.WriteLine("Found Value in index " & element)
Else
Console.WriteLine("Value Not Found")
End If
End Sub
Shared Function LinearSearch(ByVal key As Integer, _
ByVal numbers As Integer()) As Integer
Dim n As Integer
" structure iterates linearly through array
For n = 0 To numbers.GetUpperBound(0)
If numbers(n) = key Then
Return n
End If
Next
Return -1
End Function " LinearSearch
End Class
269 875 968 591 930 801 98 760 596 715 433 655 902 602 257 186 470 856 277 109
Value Not Found
Reverse the contents of an array
Public Class Tester
Public Shared Sub Main
Dim arrayReverse() As String = {"A", "B", "C", "D", "E"}
For Each fruit As String In arrayReverse
Console.WriteLine(fruit)
Next fruit
Array.Reverse(arrayReverse)
For Each fruit As String In arrayReverse
Console.WriteLine(fruit)
Next fruit
End Sub
End Class
A
B
C
D
E
E
D
C
B
A