Thursday, April 6, 2017

Binary Search: Program Code ( visual basic )

Computer Science(9608) notes and Resources





I have written code for binary search in three different ways. 







1. Normal method but, here i have passed array value in parameter. This concept might be important for 9608.



Module Module1

    Sub Main()
        Dim item(5) As Integer
        Dim counter, temp, searchitem As Integer
        Console.WriteLine(" Enter 5 items: ")
        For counter = 1 To 5
            item(counter) = Console.ReadLine
        Next
        Console.Write(" Enter item to be searched: ")
        searchitem = Console.ReadLine()
        temp = BinarySearch(item, 1, 5, searchitem)
        If temp = -1 Then
            Console.WriteLine(" Item not found")
        Else
            Console.WriteLine(searchitem & " found at position " & temp)
        End If
        Console.ReadLine()
    End Sub

    Function BinarySearch(ByRef List() As Integer, ByVal Low As Integer, ByVal High As Integer, ByVal SearchItem As Integer) As Integer
        Dim Index, Middle As Integer
        Index = -1
        Do While (Index = -1) And (Low <= High)
            Middle = (High + Low) \ 2
            If List(Middle) = SearchItem Then
                Index = Middle
            ElseIf List(Middle) < SearchItem Then
                Low = Middle + 1
            Else
                High = Middle - 1
            End If
        Loop
        BinarySearch = Index
    End Function
End Module










2. Normal method 


Module Module1

    Sub Main()
        Dim list(5), middle As Integer
        Dim index, high, low, searchitem As Integer
        Console.WriteLine(" Enter 5 items: ")
        For index = 1 To 5
            list(index) = Console.ReadLine
        Next
        Console.Write(" Enter an item to be searched: ")
        searchitem = Console.ReadLine()
        low = 1
        high = 10
        Index = -1
        Do While (low <= high)
            middle = (high + low) \ 2
            If list(middle) = searchitem Then
                index = middle
                Console.WriteLine(" item found at position " & middle)
                Exit Do
            ElseIf list(middle) < searchitem Then
                low = middle + 1
            Else
                high = middle - 1
            End If
        Loop
        If (low > high) Then
            Console.WriteLine("not found")
        End If
        Console.ReadLine()
    End Sub

End Module










3. Recursive method

Module Module1
    Dim found As Integer
    Dim searchdata(5) As Integer
    Dim searchitem As Integer

    Sub Main()
        Dim low, high As Integer
        Dim counter As Integer
        Console.WriteLine(" Enter 5 items: ")
        For counter = 1 To 5
            searchdata(counter) = Console.ReadLine()
        Next
        Console.WriteLine("search value: ")
        searchitem = Console.ReadLine()
        low = 1
        high = searchdata.Length() - 1
        BinarySearch(low, high)
        If found = 0 Then
            Console.WriteLine(" Item not found")
        Else
            Console.WriteLine(" Item found at position " & found)
        End If
        Console.ReadLine()
    End Sub


    Function BinarySearch(low, high)

        Dim middle As Integer
        If low > high Then 
            Return 1
        End If 
        middle = ((high + low) / 2)
        If searchdata(middle) = searchitem Then
            found = middle
        ElseIf searchdata(middle) < searchitem Then
            BinarySearch(middle + 1, high)
        ElseIf searchdata(middle) > searchitem Then
            BinarySearch(low, middle - 1)
        End If
        Return 1

    End Function


End Module





                                                           - CIE A-LEVEL COMPUTER SCIENCE (  9608 )

No comments:

Post a Comment

Comments here....