Python Program to implement Linear Search
source link: https://www.geeksforgeeks.org/videos/python-program-to-implement-linear-search/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Python Program to implement Linear Search
- 210 Views
- 22/08/2022
In this video, we will learn how to implement linear search using Python.
We will be using 2 approaches to implement the linear search:
1. Iterative approach
2. Recursive Approach
Iterative approach: In this approach, using for loop, we iterate on elements of an array and compare them with the required element to search. If it matches, we terminate the loop. If no element match is found, we print that element is not found in the array.
Recursive approach: In this approach, we iterate on elements of an array using tail recursion.
The base conditions are,
1. If the element is found at the current index, we return true.
2. If the current index is equal to the length of the array, we return False.
We pass the array, the target element and the starting index to search from the recursive function and the function either returns True or False, denoting whether the element is present in the array or not.
Python Program for Linear Search:
https://www.geeksforgeeks.org/python-program-for-linear-search/
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK