Python Tutorial @ Home

# Python Searching and Sorting|Sequential Search /Linear Search

Sequential Search /Linear Search

The simplest search technique is the sequential search. In this technique we start at the beginning of the list / array / table / file and search for the desired element by matching the value with each record being traversed, the process is continued till either the desired record is found or list is completely traversed.
This technique is suitable for a table which is arranged in the form of an array or linked list. This search may be improved if the list is ordered / sorted.

Advantages and disadvantages of Linear search

Advantages:

* The linear search is simple
* It is very easy to understand and implement
* It does not require the data in the array to be sorted in any particular order.
* It is memory efficient.

Disadvantages

* The linear search in inefficient.
* It is slower
* It needs more space and time complexity.

Sequential Search / Linear Search Program

Question:1
Python program to assume a list of elements. Further, search an element from the list. (Without Using Functions)
Sol:

```n=[10,20,50,32,156,57,243,122,615,129]
print("List is ")
print(n)
item=int(input("Enter the element to search "))
i=0
z=False
while(i<len(n)):
if(n[i] == item):
z=True
break
i= i + 1
if(z==True):
print("element found ")
else:
print("element not found")```

Output:

case 1:
List is
[10, 20, 50, 32, 156, 57, 243, 122, 615, 129]
Enter the element to search 32
element found

case 2:
List is
[10, 20, 50, 32, 156, 57, 243, 122, 615, 129]
Enter the element to search 125
element not found

Question:2
Python program to assume a list of elements. Further, search an element from the list and print its position. (Without Using Functions)
Sol:

```n=[10,20,50,32,156,57,243,122,615,129]
print("List is ")
print(n)
item=int(input("Enter the element to search "))
i=0
z=False
while(i<len(n)):
if(n[i] == item):
z=True
print("element found at ",i+1," position ")
i= i + 1
if(z==False):
print("element not found")```

Output:

case 1:
List is
[10, 20, 50, 32, 156, 57, 243, 122, 615, 129]
Enter the element to search 32
element found at 4 position

case 2:
List is
[10, 20, 50, 32, 156, 57, 243, 122, 615, 129]
Enter the element to search 240
element not found

Important Pages