Sunday, November 17, 2024
Google search engine
HomeLanguagesPython Program for Linear Search

Python Program for Linear Search

Python program for linear search; In this tutorial, you will learn how to create linear search program in python.

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.

Python Program for Linear Search

  • Python Program For Linear Search Using While Loop
  • Python Program For Linear Search Using For Loop
  • Linear Search in Python Program using Recursion

Python Program For Linear Search Using While Loop

# 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.

Python Program For Linear Search Using For Loop

# 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

# 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.

Recommended Python Programs

  1. Python Program to Add Two Numbers
  2. Python Program to Find/Calculate Sum of n Numbers
  3. Python Program to Find/Calculate Average of 3, 4, 5…n numbers
  4. Python Program to Print ASCII Value of Character
  5. Write a Program to Calculate Simple Interest in Python
  6. Python Program to Compute Compound Interest
  7. Leap Year Program in Python
  8. Python Program to Print Star Pattern
  9. Number Pattern Programs in Python
  10. Python Program to Print Even and Odd numbers From 1 to N
  11. Python Abs() Function: For Absolute Value
  12. How to Check Whether a Number is Fibonacci or Not in Python
  13. Python: Program to Find Power of Number
  14. Python Program to Reverse a Numbers
  15. Python Program to Find Smallest/Minimum of n Numbers
  16. Python Program to Find Largest/Maximum of n Numbers
  17. Python Program to Find The Net Bill Amount After Discount
  18. Python Program to Print Numbers From N to 1 and 1 to N
  19. Python Program to Print Numbers Divisible by 3, 5, 7
  20. Python Program to Print Prime Number 1 to N
  21. How to Find Square of Number in Python
  22. Python Program to Calculate Cube of Number
  23. Python Program to Find LCM of Two Numbers
  24. BMI (Body Mass Index) Calculator in Python
  25. Palindrome Program in Python using while loop, Function, etc
  26. Python: Program to Count Total Number of Bits in Number
  27. Python Random Number Generator Code
  28. Python Program to Calculate n-th term of a Fibonacci Series
  29. Zip Zap Zoom Python Program
  30. Python: program to convert Celsius to Fahrenheit
  31. Python Program to Find Sum of Series 1/1! 2/2! 3/3! …1/n!
  32. Python: Program to Convert Decimal to Binary, Octal and Hexadecimal
  33. Python Program to Find Roots of Quadratic Equation
  34. Python Program to Print Alphabets from A to Z in Uppercase and Lowercase
  35. Python Program to Check Given Input is Alphabet, Number or Special Character
  36. Python Program to Check IF a Number is Power of Another Number
  37. Python Check Binary Representation of Given Number is Palindrome or Not
  38. Python Program to Draw a Pie Chart
  39. Python Program Input the Radius of Circle and Compute the Area
  40. Python Program to Calculate the Area of a Rectangle
  41. Python Program to Calculate Area of Triangle
  42. Python Program to Find Area and Circumference of Circle using Radius
  43. Python Program that Accepts Marks in 5 Subjects and Outputs Average Marks
  44. Python Program to Print Binary Value of Numbers From 1 to N
  45. Python Program to Find Sum Of Even and Odd numbers From 1 to N
  46. Python Program to Calculate Simple and Compound Interest
  47. Python Program to Find Factors of a Number
RELATED ARTICLES

Most Popular

Recent Comments