In this python post, you will learn the following:
- What is a Linear Search?
- Linear Search Algorithm
- Write a Python Program For Linear Search Using While Loop
- Write a Python Program For Linear Search Using For Loop
- Linear Search in Python Program using Recursion
What is a Linear Search?
First of all, A linear search, also known as a sequential search, this method is used to find an element within a list or array. It checks each element of the list one by one / sequentially until a match is found or the whole list has been searched.
Linear Search Algorithm
Implement linear search following the below steps:
- Traverse the list/array using a loop.
- In every iteration, associate the
target
value with the given value of the list/array.- If the values match, return the current index of the list/array.
- Otherwise, move on to the next array/list element.
- If no match is found, return
-1
.
Write a Python Program For Linear Search Using While Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | # python program for linear search using while loop #define list lst = [] #take input list size num = int ( input ( "Enter size of list :- " )) for n in range (num): #append element in list/array numbers = int ( input ( "Enter the array of %d element :- " % n)) lst.append(numbers) #take input number to be find in list x = int ( input ( "Enter number to search in list :- " )) i = 0 flag = False while i < len (lst): if lst[i] = = x: flag = True break i = i + 1 if flag = = 1 : print ( '{} was found at index {}.' . format (x, i)) else : print ( '{} was not found.' . format (x)) |
After executing the program, the output will be:
Enter size of list :- 5 Enter the array of 0 element :- 10 Enter the array of 1 element :- 23 Enter the array of 2 element :- 56 Enter the array of 3 element :- 89 Enter the array of 4 element :- 200 Enter number to search in list :- 89 89 was found at index 3.
Write a Python Program For Linear Search Using For Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | # python program for linear search using for loop #define list lst = [] #take input list size num = int ( input ( "Enter size of list :- " )) for n in range (num): #append element in list/array numbers = int ( input ( "Enter the array of %d element :- " % n)) lst.append(numbers) #take input number to be find in list x = int ( input ( "Enter number to search in list :- " )) i = 0 flag = False for i in range ( len (lst)): if lst[i] = = x: flag = True break if flag = = 1 : print ( '{} was found at index {}.' . format (x, i)) else : print ( '{} was not found.' . format (x)) |
After executing the program, the output will be:
Enter size of list :- 6 Enter the array of 0 element :- 25 Enter the array of 1 element :- 50 Enter the array of 2 element :- 100 Enter the array of 3 element :- 200 Enter the array of 4 element :- 250 Enter the array of 5 element :- 650 Enter number to search in list :- 200 200 was found at index 3.
Linear Search in Python Program using Recursion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | # python program for linear search using for loop #define list lst = [] #take input list size num = int ( input ( "Enter size of list :- " )) for n in range (num): #append element in list/array numbers = int ( input ( "Enter the array of %d element :- " % n)) lst.append(numbers) #take input number to be find in list x = int ( input ( "Enter number to search in list :- " )) # Recursive function to linear search x in arr[l..r] def recLinearSearch( arr, l, r, x): if r < l: return - 1 if arr[l] = = x: return l if arr[r] = = x: return r return recLinearSearch(arr, l + 1 , r - 1 , x) res = recLinearSearch(lst, 0 , len (lst) - 1 , x) if res ! = - 1 : print ( '{} was found at index {}.' . format (x, res)) else : print ( '{} was not found.' . format (x)) |
After executing the program, the output will be:
Enter size of list :- 5 Enter the array of 0 element :- 14 Enter the array of 1 element :- 25 Enter the array of 2 element :- 63 Enter the array of 3 element :- 42 Enter the array of 4 element :- 78 Enter number to search in list :- 78 78 was found at index 4.
No comments:
Post a Comment