Python Programming - Linear Search On List

You can learn about List Manipulation in Python Programs with Outputs helped you to understand the language better.

Python Programming – Linear Search On List

Locating a particular data item in a list is known as Searching. The search application occurs in a computer when a particular data item is required from a set of stored data. The search is said to be successful if the desired data is located otherwise the search is considered to be unsuccessful.
Linear or Sequential searching is finding or searching an element in a linear i.e. one after the other or traversing each and every element and checking if it is the one required. In a sequential search, each element of a list is compared one by one with the given ITEM to be searched, until either the desired element is found or the end of the list is reached.

Python Programming - Linear Search On List chapter 7 img 1

Let us take an example to understand linear search in a better way. Consider the unsorted list shown in Figure 7.14 which contains n elements. Suppose the element that is to be searched is 9. The concept of sequential search states that element 9 is matched with each aspect of the list. If it matches, then the search is completed. Otherwise, the next element from the list is fetched and compared. This process is continued until either the desired element is found or the list is exhausted. (See Figure 7.14).

To access individual elements of a list, you should use the index position. The first location or index of a list is 0. So element 9 is located at index position or location 3.
Sequential search is a simple and easy method but is efficient only for the small size of the list and highly inefficient for the large-sized list because if the list is big, then the number of comparisons is more. In the worst case, you will have to make N comparisons, to search for the record at the end of the list, where N is the maximum number of elements in the list.

Example
Searching Element in a List Using for Loop.

# Searching element in a list
list_of_elements = [14, 12, 18, 19, 13, 17]
print(“list of items is”, list_of_elements)
x = int(input(“Enter number to search: “))
found = False
for i in range(len(list_of_elements)):
if(list_of_elements[i] == x):
found = True
print(“%d found at %dth position”%(x,i))
break
if(found == False):
print(“%d is not in list”%x)RUN
>>>
list of items is [14, 12, 18, 19, 13, 17]
Enter number to search: 13
13 found at 4th position
>>>

Example
Searching Element in a List Using while Loop.

# Searching element in a list
items = [2, 8, 9, 12, 17]
print(“list of items is”, items)
x = int(input(“enter item to search:”))
i = flag = 0
while i < len(items):
if items[i] == x:
flag = 1
break
i = i + 1if flag = = 1:
print(x,”found at position:”, i)
else:
print(x,”not found in a list”)RUN
>>>
list of items is [2, 8, 9, 12, 17]
enter item to search:12
12 found at position: 3list of items is [2, 8, 9, 12, 17]
enter item to search:7
7 not found in a list
>>>

Leave a Reply

Your email address will not be published. Required fields are marked *