Phoenix Ignited is a proud tech solutions partner of Luxauro.com. View their news and media page here: Luxauro.com

Linear Search Algorithm

Linear Search Algorithm

The Linear Search Algorithm is the most fundamental / “base” search algorithm. It is incredibly simple and straightforward and in fact you use it every day, most often without even realizing it. The Linear Search Algorithm takes a “target” value of a set of data, and searches through the ENTIRE set of data until it finds the target value. Upon finding the target value the algorithm returns the index of the target value in the set of data. The Linear Search Algorithm has a Algorithmic Time Complexity of O(n), since it searches through the entire data set until it finds the target value. In python it could look like: 

							
							
					def linear_search(arr, target):
    """
    Perform a linear search on the given list.

    :param arr: List of elements to search.
    :param target: The element to find in the list.
    :return: The index of the target element if found, otherwise -1.
    """
    for index, element in enumerate(arr):
        if element == target:
            return index
    return -1

# Example usage
arr = [4, 2, 7, 1, 9, 3]
target = 7

result = linear_search(arr, target)

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

You can play around with this by creating a .py file and running it on your machine. Change up the data set, the target value, etc. 

That’s it on the Linear Search Algorithm, I’ll be exploring other algorithms in the near future!

Walter Miely is a tech entrepreneur and CEO of Phoenix Ignited Tech You can find him on Linkedin. This material is licensed under the CC BY 4.0 License LEGAL DISCLAIMER: The content provided here is provided AS IS, and part of, or the entirety of this content may be incorrect. Please read the entireLegal Disclaimer here.

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.