Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified modules/searches/recursive_linear_search.f90 #19

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions examples/searches/recursive_linear_search.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
!> Example program demonstrating Recursive Linear Search
!!
!! This program demonstrates the use of the `recursive_linear_search_module` to search for target values within arrays.

program recursive_linear_search_example
use recursive_linear_search_module
implicit none

integer, dimension(5) :: array

array = (/ 306, 1005, 5, 62, 0 /)

!! Search for the number 62 in the array
print*, "Target = 62: ", recursive_linear_search(array, size(array), 62) !! Prints 4.

!! Search for the number 10 in the array
print*, "Target = 10: ", recursive_linear_search(array, size(array), 10) !! Prints -1 because item 10 is not found.

end program recursive_linear_search_example
43 changes: 15 additions & 28 deletions modules/searches/recursive_linear_search.f90
Original file line number Diff line number Diff line change
@@ -1,47 +1,34 @@
!> Recursive Linear Search
!> Recursive Linear Search Module
!!
!! This is the recursive implementation of the linear search algorithm in Fortran.
!! This module contains a recursive implementation of the linear search algorithm.
!! It includes the `recursive_linear_search` function to search for a target value within a given collection.

program recursive_linear_search_program
module recursive_linear_search_module
implicit none
contains

integer, dimension(5) :: array

array = (/ 306, 1005, 5, 62, 0 /)

!! Search for the number 62 in array.
print*, "Target = 62: ", recursive_linear_search(array, size(array), 62) !! Prints 4.

!! Search for the number 10 in array.
print*, "Target = 10: ", recursive_linear_search(array, size(array), 10) !! Prints -1 because item 10 is not found.

contains

!! This function recursively searches for a target in a given collection.
!! This recursive function searches for a target in a collection.
!! Returns the index of the found target or -1 if target is not found.
recursive function recursive_linear_search(collection, index, target) result(target_index)
!! recursive keyword declares that the function is recursive.
!! result keyword specifies the function result.

integer, dimension(:), intent(in) :: collection !! A collection for elements of type integer
integer, intent(in) :: index !! Initially collection size.
integer, intent(in) :: target !! Target value to be searched.
integer :: target_index !! Target's index in the collection to return.
integer, dimension(:), intent(in) :: collection !! A collection of integer elements
integer, intent(in) :: index !! The current index in the collection
integer, intent(in) :: target !! The target value to be searched
integer :: target_index !! The index where the target is found

!! Stop recursive calls with "if size is 0 and item not found" base condition.
!! Base case: if index is 0 and target is not found, return -1
if (index .eq. 0) then
target_index = -1
return
end if

!! Check if the target is at the current index
if (collection(index) .eq. target) then
!! Set target index if target found.
target_index = index
else
!! Recursively call the function with (index - 1) for index argument.
else
!! Recursively search in the remaining part of the collection
target_index = recursive_linear_search(collection, index-1, target)
end if

end function recursive_linear_search

end program recursive_linear_search_program
end module recursive_linear_search_module