new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
18
Task/Binary-search/Fortran/binary-search-1.f
Normal file
18
Task/Binary-search/Fortran/binary-search-1.f
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
recursive function binarySearch_R (a, value) result (bsresult)
|
||||
real, intent(in) :: a(:), value
|
||||
integer :: bsresult, mid
|
||||
|
||||
mid = size(a)/2 + 1
|
||||
if (size(a) == 0) then
|
||||
bsresult = 0 ! not found
|
||||
else if (a(mid) > value) then
|
||||
bsresult= binarySearch_R(a(:mid-1), value)
|
||||
else if (a(mid) < value) then
|
||||
bsresult = binarySearch_R(a(mid+1:), value)
|
||||
if (bsresult /= 0) then
|
||||
bsresult = mid + bsresult
|
||||
end if
|
||||
else
|
||||
bsresult = mid ! SUCCESS!!
|
||||
end if
|
||||
end function binarySearch_R
|
||||
23
Task/Binary-search/Fortran/binary-search-2.f
Normal file
23
Task/Binary-search/Fortran/binary-search-2.f
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
function binarySearch_I (a, value)
|
||||
integer :: binarySearch_I
|
||||
real, intent(in), target :: a(:)
|
||||
real, intent(in) :: value
|
||||
real, pointer :: p(:)
|
||||
integer :: mid, offset
|
||||
|
||||
p => a
|
||||
binarySearch_I = 0
|
||||
offset = 0
|
||||
do while (size(p) > 0)
|
||||
mid = size(p)/2 + 1
|
||||
if (p(mid) > value) then
|
||||
p => p(:mid-1)
|
||||
else if (p(mid) < value) then
|
||||
offset = offset + mid
|
||||
p => p(mid+1:)
|
||||
else
|
||||
binarySearch_I = offset + mid ! SUCCESS!!
|
||||
return
|
||||
end if
|
||||
end do
|
||||
end function binarySearch_I
|
||||
Loading…
Add table
Add a link
Reference in a new issue