×

Linear Search

Searching: In the data structure, searching is the process in which an element is searched in a list that satisfies one or more than one condition.

Types of searching

There are two types of searching in the data structure.

  1. Linear searching
  2. Binary searching

Linear searching

In this searching technique, the searching element is compared one by one with each element of the list until the element is found. It is also called sequential searching.

In this, the searching element is compared with the first element of the list. If both the elements are the same, it returns the index value of that element. Otherwise, it returns -1. Similarly, the entire list is compared until the searching element is found. If the element is not found even after comparing the entire list, the searching prints "unsuccessful" message.

It is a very simple searching technique, but it takes a lot of time because the average-case complexity of the linear search is O (n).

Complexity of Linear searching

ComplexityBest caseAverage caseWorst case
TimeO(1)O(n)O(n)
Space  O(1)

Algorithm of Linear searching

Linear_searching(A, N, VAL)
Step 1: SET FLAG = -1
Step 2: SET I = 1
Step 3: Repeat step 4 while I<=N
Step 4: if A[I] = VAL
            SET FLAG = I
            print “index value of the element”
            go to step 6
            // end of if
            SET I = I + 1
            // end of loop
Step 5: if FLAG = -1
            print " element is not found in this list "
            // end of if
Step 6: exit  

For example, suppose you have the following array, and you have to search 30 in it.

Linear Search in Data Structure
Step 1: The given element (30) is compared with the first element (11) of the array.                                              
11 ? 30              Both elements are not the same. Now, it will go to the next element.  
Step 2: The given element (30) is compared with the second element (54) of the array.                                              
54 ? 30              Both elements are not the same. Now, it will go to the next element.
Step 3: The given element (30) is compared with the third element (45) of the array.                                              
45 ? 30              Both elements are not the same. Now, it will go to the next element.  
Step 4: The given element (30) is compared with the fourth element (30) of the array.                                              
30 = 30              The given element is found, so it will stop comparing. It returns the index value, that is 3.

Linear search program in C language

#include<stdio.h>    
void main ()   {       
int a[10] = {15, 13, 40, 51, 32, 80, 14, 13, 57, 19};       
int element, i, flag;       
printf("\n enter the searching element \n");      
scanf("%d",&element);       
for (i = 0; i< 10; i++)      
 {         
  if(a[i] == element)         
   {              
 flag = i+1;               break;  

        }       
     else       
     flag = 0;   
   }        
if(flag != 0)    
   {        
   printf("\n element found and index value is %d\n",flag); 
      }     
  else   
    {    
       printf("\n element not found\n");   
     }  
}     

Output

enter the searching element
40 
element found and index value is 2

Related Topics

Given a Binary Tree, find its Minimum Depth

Implementation // Creating a C++ program or implementation to search and explore the minimum depth of a given binary tree.  #include<bits/stdc++.h> using namespace std; // Creating a new binary tree node struct __nod { int record; struct __nod*...

5 minutes read.

Find Number of Minimum Insertion to Make a String Palindrome

You have been given a string. You have to find out the number of minimum insertions to make this string palindrome. The string will contain only lower case alphabets. Note:What is...

4 minutes read.

Boruvkas algorithm

This algorithm is used for finding minimum spanning tree from a weighted graph. Like prim’s and kruskal’s algorithm it is also a greedy algorithm. Note:What is the minimum spanning tree?We know...

4 minutes read.

Stack vs Array

Difference between Array and Stack In this article, we are going to discuss the major differences between the stack and array data structures: Array – In the data structure, the array is...

3 minutes read.

What are Forest Trees in Data Structure

Data structure A data model manages and optimizes computer resources, and a database stores and manages data. It's one of many uses for data structures to hold data. Data structures come...

5 minutes read.

Flattening a Linked List

In this article, we are going to study about the logic behind the flattening of linked list and we also going to build a code in the C++ to flatten...

3 minutes read.

Time Complexity of Selection Sort in Data Structure

What is Time Complexity? The term “Time complexity” can be defined as the number of times executions made of a particular sequence of instructions and not the total amount of time...

3 minutes read.

Data Structures Algorithms

What is an Algorithm? An algorithm is a sequence of steps used to complete a job or get a desired result. It is similar to programming building elements that let cell...

4 minutes read.

Binary tree insertion

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. Here the name of the tree itself portrays the mechanism...

4 minutes read.

Find Bridges in a Graph

You have been given a graph. You have to find out the bridges in that graph. Graph may be connected or disconnected. You have to print vertices of particular edge...

4 minutes read.

Deque in Data Structure

Deque A deque referred as “Double-Ended Queue”, is a linear collection of data items same like queue data structure. deque has two ends, front end and rear end, deque is the...

27 minutes read.

Serialize and Deserialize a Binary Tree

Implementation // Writing a C++ program to check the serialization and deserialization of binary tree.   #include <iosstream> /* A binary tree node contains a key and a pointer to the left and right...

4 minutes read.

Data structure: Infix to Prefix Conversion

Infix to Prefix Conversion In present time, we use the infix expression in our daily life but the computers are not able to understand this format because they need to keep...

4 minutes read.

Segregate Even and Odd nodes in a Linked List

Segregate even and odd nodes in a Linked List In this problem, we have given a linked list with integer numbers. We need to modify the given linked list in such...

4 minutes read.

Operations on 2D-Arrays

Two Dimensional Array Operations Adding Elements to Two-D Arrays We must put data in both rows and columns when inserting items in 2-D Arrays. As a result, we employ the idea of...

10 minutes read.

What Is Dfs Algorithm in Data Structures

DFS stands for Depth First Search. Generally, it is a repetitive or decidable type of algorithm which is basically used in identifying all the vertices or nodes of a graph...

5 minutes read.

Convert Binary Tree into a Threaded Binary Tree

Implementation /*Writing a C++ program that will help us change the binary tree into a threaded binary tree and help us transform. */ #include <bits/stdc++.h> using namespace std; /*Creating the structure of a node...

11 minutes read.

Sparse Matrix in Data Structure

Sparse Matrix The sparse matrix is a two-dimensional data object which is made by m rows and n columns, so we can say the number of data values in sparse matrix...

6 minutes read.

What is a 2-3 Tree in Data Structure?

Tree Data structure The information about the tree is self-explanatory. Trees are ordered and, therefore, not linear. But they are actually designed differently. Tree A node-based data model that represents and...

5 minutes read.

Cocktail Sort

C Program executes cocktail sort. Combo sort is a somewhat straightforward arranging calculation initially planned by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered by Stephen Lacey and Richard Box...

5 minutes read.