Skip to content

added linear search using recursion - #9

Open
ehabrafat wants to merge 1 commit into
OpenSourceOrganizations:mainfrom
ehabrafat:main
Open

added linear search using recursion#9
ehabrafat wants to merge 1 commit into
OpenSourceOrganizations:mainfrom
ehabrafat:main

Conversation

@ehabrafat

Copy link
Copy Markdown

Description
Implemented linear search algorithm using recursion in Java. This provides an alternative recursive approach to the classic linear search problem.

Type of Change
New algorithm implementation

Language
Java

Problem Details

  • Problem Name: Linear Search
  • Difficulty: Easy
  • File Added/Modified: LinearSearchUsingRecursion.java

Time/Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(n)

Approach
The implementation uses recursion to traverse through the array elements:

  • Base Case 1: If we reach the end of the array, return -1 (element not found)
  • Base Case 2: If current element matches the key, return the current index
  • Recursive Case: Otherwise, recursively call the function with the next index
    The public search() method serves as a clean interface that initializes the recursive process starting from index 0.

Testing

  • I have tested my solution locally
  • I have included test cases in my implementation
  • My solution handles edge cases appropriately
  • I have verified the solution works with the provided examples

Additional Notes

  • While iterative linear search has O(1) space complexity, this recursive version demonstrates how recursion can be applied to simple search problems.
  • The implementation handles edge cases including empty arrays and elements not found in the array.
  • This serves as a good educational example for understanding recursion in the context of search algorithms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant