Linear Search Python (original) (raw)

Last Updated : 07 Apr, 2025

Given an array, **arr of n elements, and an element **x, find whether element **x is present in the array. Return the index of the first occurrence of **x in the array, or -1 if it doesn’t exist.

**Examples:

**Input: arr[] = [10, 50, 30, 70, 80, 20, 90, 40], x = 30
**Output : 2
**Explanation: For array [10, 50, 30, 70, 80, 20, 90, 40], the element to be searched is 30 and it is at index 2. So, the output is 2.

**Explanation: A simple approach is to do a **linear search, i.e

**Using Iterative Approach

This takes an array arr and a target element to search. Iterates through each element of the array and compares it with the target. If a match is found, it returns the index of the element. If the target is found, it returns its index; otherwise, it returns -1.

Python `

def linear_search(arr, target): for index in range(len(arr)): if arr[index] == target: return index return -1

Example

arr = [10, 23, 45, 70, 11, 15] target = 70

result = linear_search(arr, target)

if result != -1: print(f"Element found at index: {result}") else: print("Element not found in the array")

`

Output

Element found at index: 3

Table of Content

**Using Recursive Approach

def linear_search_recursive(arr, target, index=0): if index == len(arr): return -1 if arr[index] == target: return index

# Recursive call to check the next element
return linear_search_recursive(arr, target, index + 1)

Example

arr = [10, 20, 30, 40, 50] target = 30

result = linear_search_recursive(arr, target)

if result != -1: print(f"Element {target} found at index {result}.") else: print(f"Element {target} not found in the list.")

`

Output

Element 30 not found in the list.

**Time complexity: O(n).

**Auxiliary Space: O(1) for iterative and O(n) for recursive.

Please refer complete article on Linear Search and Difference Between Recursive and Iterative Algorithms for more details!

**Using RegEx Method

This function takes a list lst and a pattern as input

import re

def linear_search(lst, pattern): # Compile the pattern using re regex = re.compile(pattern)

for index, element in enumerate(lst):
    if regex.search(element):
        return f"Pattern found in element '{element}' at index {index}"

return "Pattern not found in the list"

Example list

my_list = ["apple", "banana", "cherry", "date", "elderberry"] pattern = "an" result = linear_search(my_list, pattern) print(result)

`

Output

Pattern found in element 'banana' at index 1

**Time Complexity: The time complexity of this program is O(n) because we need to traverse the entire list to convert it to a string and count the number of commas before the match. The search using regular expression can also take some time depending on the length of the string, but it is generally considered to be a fast operation.

**Space Complexity: The space complexity of this program is O(n) because we need to store the entire list as a string in memory. Additionally, the regular expression module uses some memory for its operations. However, since the input size is relatively small in this case, the space complexity is not a major concern.