! binary search program
program mid_ex_92
implicit none
integer :: low, high, search_key, search_result, &
middle, size
integer, allocatable :: mtx_a(:)
write(*,*) 'Enter the dimension of matrix A which is less than
1000'
read (*,*) size
do ii = 1:size
mtx_a(ii) = 2*(ii-1)
end
write(*,30) size
30 format(2x,'Enter a number between 0 and ',I3,': ')
read (*,*) search_key
low = 0
high = size
search_result = -1
while ( low <= high & search_result == -1 )
middle = ( low + high ) / 2;
if ( search_key = mtx_a(middle) )
search_result = middle
elseif ( search_key < mtx_a(middle) )
high = middle - 1
else
low = middle + 1
end
end while
if ( search_result /= -1 )
write(*,10) search_key, search_result
10 format(2x, I3, ' found in array elemnet ', I3)
else
write(*,20) search_key
20 format(2x, I3,' not found')
end
end mid_ex_92
¡@