In computer science, in data structure we need to implement the concept of Searching. It is basically used to search a particular item from a data structure.
In this article, we will discuss about Sequential Search which is popularly known as Linear Search. In this algorithm we search the element from the starting index element and then it will check sequentially the next element.
Basic Understanding of Linear Search
Step 1: Start
Step 2: Take the total number of user-given inputs (size)
Step 3: Take the elements from the user and store them within a memory
Step 4: Take the element that the user wants to search (element)
Step 5: Start matching from the first element to the last element
Step 6: If the element is present in the memory, then print “Element is Found”. If the element is not present in the memory, then print “Element is not Found”.
Step 7: Stop
How Linear Search works here?
In this diagram, we have a data structure (Array) to store 5 elements i.e. 10, 40 50, 8, 9. Now our task is to find an element 50 from that array. Here, Linear Search is a searching mechanism that is used to search 50 from that given array. Here in the beginning, Linear Search will search if there 50 is present at index 0 or not. In index 0, the element that is present is 10 that is not equals to 50, then we have to check with the next value (40) that is present in the next index that is index 1. In index 1, the element that is present is 40 that is not equals to 50. Similarly, we have to check with the next value (50) that is present in the next index that is index 2. In index 2, the element that is present is 50 that is equals to 50. So, we in this easy method, we can search an element within an array.
Linear Search using C Programming
#include <stdio.h>
//Solution from https://codewindow.in
int main(void) {
int arr[10],size, element,flag=0;
printf("How many elements you want to store? ");
scanf("%d",&size);
//Store the user given elements with in an array
for(int index=0; index<size;index++){
scanf("%d",&arr[index]);
}
printf("Which element you want to search? ");
scanf("%d",&element);
//Concept of Linear Search
for(int index=0; index<size;index++){
if(arr[index]==element){
flag=1;
break;
}
}
//Check the flag value
if(flag==1)
printf("Element is found");
else
printf("Element is not found");
return 0;
}
//Solution from https://codewindow.in
Custom Input:
How many elements you want to store? 5
10 23 45 2 4
Which element you want to search? 23
Custom Output:
Element is found
Linear Search using Python Programming
size=int(input("How many elements you want to store? "))
#Solution from https://codewindow.in
#Store the user given elements with in an array
arr=[]
for i in range(size):
arr.append(int(input("Enter your element")))
#Take the element from the user
element=int(input("Which element you want to search? "))
#Concept of Linear Search
flag=0
for index in range(len(arr)):
if arr[index]==element:
flag=1
break
#Checking the value of flag
if flag==1:
print("Element is found")
else:
print("Element is not found")