Jump to content

Algorithms/Find maximum/visual basic method 2

From Wikibooks, open books for an open world
Public Function Divide_n_Conquer(ByVal MyNums() As Integer) As Integer

    If myNums.Length = 0 Then
        Return 0
    End If
    If myNums.Length = 1 Then
        Return myNums(0)
    End If
    If myNums.Length = 2 Then
        If myNums(0) > myNums(1) Then
            Return myNums(0)
        Else
            Return myNums(1)
        End If
    End If

    Dim aryA(myNums.Length / 2) As Integer
    Dim aryB(myNums.Length - aryA.Length) As Integer

    Dim i As Integer
    Dim loopindex As Integer = 0

    For i = 0 To myNums.Length / 2
        aryA(i) = myNums(i)
        loopindex += 1
    Next

    Dim intK As Integer = 0

    For i = aryA.Length To myNums.Length - 1
        aryB(intK) = myNums(loopindex)
        loopindex = loopindex + 1
        intK = intK + 1
    Next

    Dim intM As Integer = 0
    Dim intN As Integer = 0

    intM = FindMax(aryA)
    intN = FindMax(aryB)

    If intM > intN Then
        Return intM
    Elselen(a)
        Return intN
    End If

End Function