Python Program For Linear Search (With Code & Explanation)

Python Program For Linear Search

In this tutorial, you will learn about the python program for linear search.

In this article, we will explore the concept of linear search in Python.

Linear search is a simple searching algorithm that traverses through a list or array in a sequential manner to find a specific element.

In linear search, we compare each element of the list with the target element until we found a match or we have traversed the entire list.

We use this algorithm mainly for small-sized arrays or unsorted lists.

Let’s dive into the details of the Python program for linear search.

Section 1

Algorithm: Python Program for Linear Search

Here is the python program for linear search.

Python Program for Linear Search

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

The given Python program demonstrates the implementation of linear search.

It takes an array arr and a target element target as input.

The linear_search() function iterates through each element of the array using a for loop.

Inside the loop, it compares each element with the target element.

If we found a match, we return the index of the element.

If we have traversed the entire array and we found no match, the function returns -1.

Section 2

Implementation: Python Program for Linear Search

Here is the complete implementation of linear search with an example.

Python Program for Linear Search

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

# Example usage
my_list = [4, 2, 7, 1, 9, 5]
target_element = 7

# Calling the linear_search function
result = linear_search(my_list, target_element)

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

You can run this code on our free Online Python Compiler.

Output

The target element 7 is found at index 2 in the list.

In this example, we have a list called my_list containing some elements [4, 2, 7, 1, 9, 5].

We want to find the index of the target element 7 in this list.

We call the linear_search() function and pass the my_list and the target_element as arguments.

The function iterates through each element of the list and checks if it matches the target element.

In this case, when the loop reaches the element 7, it matches the target element, and the function returns the index of 7, which is 2.

Finally, we check the result returned by the linear_search function.

If the result is not -1, we print a message stating that the states that we have found the target element.

Otherwise, we print a message stating that we have found the target element in the list.

Section 3

Detailed Explanation: Python Program for Linear Search

Let’s break down the Python program and understand its functionality in detail.

Defining the linear_search() Function

The program starts by defining the linear_search() function.

This function takes two parameters: arr (the array that we want to search) and target (the element that we want to find).

Iterating through the Array

We used the for loop to iterate through each element of the array.

The loop runs from i = 0 to i = len(arr)-1, where len(arr) gives the length of the array.

Comparing Elements: Python Program For Linear Search

Inside the loop, we compare each element of the array with the target element using the == operator.

If the current element matches the target element, the function immediately returns the index of that element using the return statement.

Returning -1 if No Match Found

If the loop completes without finding a match, it means the target element is not present in the array.

In such a case, the function returns -1 using the return statement outside the loop.

FAQs

FAQs About Python Program for Linear Search

Q1: What is the time complexity of linear search?

Linear search has a time complexity of O(n), where n is the number of elements in the array.

It means that the execution time of the linear search algorithm increases linearly with the size of the input.

Q2: Is linear search efficient for large arrays?

No, linear search is not efficient for large arrays as it needs to traverse through each element sequentially.

For large arrays, other search algorithms like binary search or hash-based search provide better efficiency.

Q3: Can I use linear search for sorted arrays?

Yes, you can use linear search for both sorted and unsorted arrays.

However, for sorted arrays, binary search is a more efficient option.

Q4: What happens if the target element is not found in the array?

If the program doesn’t find the target element in the array, the linear search algorithm returns -1.

This serves as an indicator that the element is not present in the array.

Q5: Can I use linear search for searching strings or objects?

Yes, you can use linear search for searching strings or objects in an array.

You can customize the comparison operation inside the linear search function to handle different data types.

Q6: Are there any alternatives to linear search?

Yes, there are several alternatives to linear search depending on the requirements and characteristics of the data.

Some popular alternatives include binary search, hash-based search, and various tree-based search algorithms.

Wrapping Up

Conclusions: Python Program for Linear Search

In this article, we explored the Python program for linear search, a simple yet effective searching algorithm.

We discussed the implementation of linear search using a Python function and provided a detailed explanation of its working.

We also answered some frequently asked questions related to linear search.

Keep experimenting with different search algorithms to find the most efficient solution for your specific use case.

Happy Coding!


Discover more from Python Mania

Subscribe to get the latest posts sent to your email.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Related Articles:

Recent Articles:

0
Would love your thoughts, please comment.x
()
x

Discover more from Python Mania

Subscribe now to keep reading and get access to the full archive.

Continue reading