Swift Program to Recursively Linearly Search an Element in an Array


In Swift, linear search is the most easiest search algorithm. It is also known as sequential search because it check all the elements of the given array or sequence one by one. If the target element is found, then return the index of that array otherwise return element not found. So lets check if the given array contains the target element or not using recursive linear search.

Algorithm

  • Step 1 − Create a function to check if the given element is present in the specified array or not using recursive linear search.

  • Step 2 − Inside the function, we use if-else conditions to check if the element is present or not.

  • Step 3 − If the element is not found, then this function will return nil.

  • Step 4 − If the element is found, then this function will return index value of that function.

  • Step 5 − If neither of the base condition meets, then the function call itself recursively with same array and target element but with the index value incremented by 1. This process continue till any of the base condition meet.

  • Step 6 − Outside the function create an array and a target element.

  • Step 7 − Now call the function to pass the array and target element in it to get the output.

Example

In the following swift example, we will create a function that takes three arguments: array, target element, and the initial index value that is 0 and returns an optional integer value that represent index if the target element is found I the array or will if the target element in not found. This function has the following base conditions −

If the current index is out of range or the target element is not found then this function return nil.

If the current element is equal to the target element, then this function return the index.

If both the condition does not match, then the function call itself recursively with the same array and target element, but the index value is incremented by 1, will effectively search the next element of the array.

This process continue till the any of the base conditions meet.

import Foundation
import Glibc

func recursiveSearch(arr: [Int], ele: Int, index: Int = 0) -> Int? {

   // If the element not found
   if index >= arr.count { 
      return nil
   } 
    
   // If the element is found
   else if arr[index] == ele {
      return index
   }
    
   // For recursive case
   else {
      return recursiveSearch(arr: array, ele: E, index: index + 1)
   }
}

// Test Case
let array = [2, 6, 2, 5, 3, 7, 4, 9, 6]
let E = 4

if let index = recursiveSearch(arr: array, ele: E) {
   print("Element: \(E) is found at index:", (index))
} else {
   print("Element: \(E) is not found")
}

Output

Element: 4 is found at index: 6

Conclusion

So this is how we recursively linearly search an element in an array. Here we use recursive approach to search elements linearly in the array which easily search specified element from the array.

Updated on: 24-Apr-2023

133 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements