The optimal binary search algorithm uses a sorted array as thedata container. Define the range to be searched using a half-closedrange of indices [begin:end). Initially, this will be thehalf-closed range of the whole array.The algorithm is as follows:If begin>=end, this would indicate the range is empty orinvalid, thus the value cannot exist. We return the end iterator toindicate value not found.If the range has one or more elements, then we calculate themiddle index of the range:middle = begin + (end - begin) / 2Note: division takes precedence over addition.We then compare the value in the middle of the range with thevalue being searched. If they are equal, we are done, we simplyreturn the middle index.Otherwise, if the value being searched is less than the middlevalue, we adjust the upper bound of the array so the next iterationonly searches the lower half of the original range:new_end = middleOtherwise we adjust the lower bound of the array so the nextiteration only searches the upper half of the range:new_begin = middle + 1We then repeat the algorithm using the adjusted half-closedrange.The algorithm can be implemented in Visual Basic as follows.Note the array, a, is passed by reference (ByRef) to prevent makingan unnecessary copy of the array. The function does not make anychanges to the array itself so there's no need to copy it.Function bin_search (ByRef a As Integer(), ByVal begin AsInteger, ByVal end As Integer, ByVal value As Integer) AsInteger' save the end iterator (it may change later)Dim not_found As Integer = end' while the range is valid...While begin < end' calculate middle indexDim middle As Integer = begin + (end - begin) / 2' compare the middle element for equalityIf value = A[middle] Then' value found!Return middleEnd If' value not found, so determine which half of array toeliminateIf value < A[middle] Then' eliminate upper half (if value exists, it must be in the lowerhalf)end = middleElse' eliminate lower half (if value exists, it must be in upperhalf)begin = middle + 1End If' repeat algorithm...End While' if we get this far, the range is either empty or invalid(begin>=end), so the value does not existReturn not_foundEnd Function