Updated May 30, 2023
Definition of Linear Search in Python
Python is one of the trending and powerful language which is used for performing many tasks related to searching like linear search or binary search. Linear search in Python makes the searching technique quite efficient and easy for any element to be searched. Linear searching in Python is also known as sequential search where the searching is performed in a sequential manner by comparing one element to the next and so on until the required element is found for manipulation and transformation. It makes the overall search using indices.
Syntax:
There is no syntax for performing linear search in Python, but some algorithmic steps are performed in order to get the elements and key values within the list which is represented as follows:
LinearSrch (lst_value, key)
for each element_val present within the list
if element_val = = some_val
return its index position
return -1
How to Perform Linear Search in Python?
Linear Search in python is also known as sequential search in which the elements are searched and compared based on the indices being allocated to them. Let’s see in some stepwise manner how exactly linear search in Python is carried out:
- A list is defined which consists of all the items or elements present within it.
- Each element present within the list will acquire a memory cell or say index where it will be present and can be considered as an address of the element.
- Now start the search in a sequential manner from the first index of the element then check for that key of the element from the list.
- If in case the element is found and is the one that is required of then that index with the element will be returned.
- On the other hands if any situation occurs where the element is not found then it can be considered that element or key is not present.
- If the key returned or the position expected is not the same, then the sequential search is not proper and there is the absence of the element expected.
- Once the linear search algorithm performed over the element is successful or not time complexity findings also play a major role as per each dry run or execution which is performed over the list to find the elements.
- Time complexity of the element is considered as per situation suppose for an instance it is always expected to have number of elements present within the list to be less than 100 and should be a small list then in that case the time complexity will come around with the best case, Average case and worst case.
- Best case time complexity of linear search algorithm comes out to be O(1), average case time complexity of linear search algorithm comes out to be O(n), and worst-case time complexity comes out to be O(n)
- If the number of elements to be applied with linear search comes out to be more than 10000 elements within the list and the element is present at the last of the entire list then it will be a worst case situation and will consume lot of time for any manipulation which is not desired off.
- More clarity on linear search performance will come once example will be implemented in the next section.
Examples of Linear Search in Python
Following are the examples are given below:
Example #1
This program demonstrates the linear search applied on the array where if the element is present in the list then it will return the elements or else it will give some error message as shown in the output.
Code:
def srch_elemnt (arr_0, n_1, x_2):
for p_0 in range (0, n_1):
if (arr_0[p_0] == x_2):
return p_0
return -1
arr_0 = [8, 10, 12, 13, 24]
x_2 = 10
n_1 = len(arr_0)
result_0 = srch_elemnt (arr_0, n_1, x_2)
if (result_0 == -1):
print ("There is no such element_present_in defined_array")
else:
print ("Element is present_within_element", result_0)
Output:
Explanation: The above function in the example tries to make a sequential search where the element is searched for performing a search where if the desired element is found then it will give a proper message otherwise it will return a message which will be an error message depicting the root cause or inappropriateness of the returned element.
Example #2
This program demonstrates the implementation of the Linear search algorithm with the help of the required list and elements either from left to right search or from right to left search as depicted below.
Code:
def srch_algo (arr_8, srch_elmnt):
lft_hand = 0
length_0 = len(arr_8)
position_1 = -1
right_hand = length_0 - 1
for lft_hand in range (0, right_hand, 1):
if (arr_8[lft_hand] == srch_elmnt):
position_1 = lft_hand
print ("Element_found_in_array_position_with ", position_1 +
1, " Position_found_with_required_position ", lft_hand + 1, " Search_made.")
break
if (arr_8[right_hand] == srch_elmnt):
position_1 = right_hand
print ("Desired_elemnt_found ", position_1 + 1,
" Position_found_element ", length_0 - right_hand, " Search_made.")
break
lft_hand += 1
right_hand -= 1
if (position_1 == -1):
print ("Required_element_not_found", lft_hand, " Search_made")
arr_8 = [12, 22, 13, 40, 25]
srch_elmnt = 13
srch_algo (arr_8, srch_elmnt)
Output:
Explanation
- These programs try to search for the desired pattern with some element required after applying a search on the list of elements.
- Sequential search is performed in order to get the elements retrieved as per requirement.
- The python function applied with search includes all the major declarations and logic with left search and right search to find the element in an efficient manner.
- This makes the process of searching streamline and less complex after implementation.
- Time complexity also comes out to be proper if the element returned is present at the correct and desired location or index as defined.
Conclusion
Linear search in Python is used for making sequential search which is used for easy finding of elements that are used in later point of time for some of the references. It has given programmers the flexibility to perform searches with ease and flexibility. Binary search and linear search is performed in a sequential manner which makes the user and the programmer interested this algorithm for enhancement.
Recommended Articles
This is a guide to Linear Search in Python. Here we also discuss the definition and how to perform a linear search in python along with different examples and its code implementation. You may also have a look at the following articles to learn more –