! Program Example from "Essential Fortran 90 & 95" by Loren P. Meissner
! Copyright 1996. Copying for sale requires permission from the author.
! Otherwise, distribution is permitted if these three lines are included.

  module F26M
    implicit none
    public :: Search
  contains
    function Search( List, Key ) result( Search_R )
      character (len = *), dimension(:), intent(in) :: List  ! Assumed-length strings
      character (len = *), intent(in) :: Key
      integer, dimension(2) :: Search_R
      integer :: Middle, Last
  ! start function Search
      Last = size( List )
      Search_R(1) = 0
      Search_R(2) = Last + 1
      do
        if (Search_R(2) - Search_R(1) > 1) then
          Middle = (Search_R(1) + Search_R(2)) / 2
          if (List(Middle) == Key) then
            Search_R = Middle   ! Assign scalar to both vector elements.
            exit
          else if (List(Middle) > Key) then
            Search_R(2) = Middle
          else
            Search_R(1) = Middle
          end if
        else
          exit
        end if
      end do
      return
    end function Search
  end module F26M

