×

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

Primitive Data Structure in C

The data structure is a logical or mathematical model for organizing and structuring the main memory or elements. We can classify the data structures in two ways one is primitive, and...

10 minutes read.

Semi-Structured data

In this article, we will discuss the semi-structured data. Data can be defined as the distinct piece of information that is gathered and translated for some purpose. It can be...

5 minutes read.

Blowfish algorithm

The Blowfish algorithm is the very first encryption algorithm which is symmetric. It was firstly used as an alternate algorithm for the DES algorithm. It was designed by Bruce Steiner...

3 minutes read.

Recursion - Factorial and Fibonacci

In this article, we will learn how to find the factorial of a number and the Fibonacci series up to n using the recursion method. What is recursion? Defining anything in terms...

7 minutes read.

Extended Binary Tree

An extended binary tree is a binary tree in which all the NILL subtrees present mainly in the original trees are exchanged with the special nodes that are primarily known...

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

Rotate a Singly Linked List

Rotate a Singly Linked List This article will explain how we can rotate the singly linked list. Here we have given a singly linked list, and we need to rotate this...

4 minutes read.

Operations of B Tree in C++ Language

B tree tends to be a self-aligning and balancing tree that helps us organise our data and document safely. We know that every data or information in the B tree...

9 minutes read.

Priority Queue in Data Structure

Priority Queue A priority queue is a special kind of queue, in priority queue we give some priority to an element and according to this priority an element can be served...

3 minutes read.

Linear Queue Data Structure in C

Data Structure There are many ways to store data in programming, that Queue has features that make it all the more special. We all know that data structure is a way...

9 minutes read.

What is Skewed Binary Tree

To understand the skewed binary tree, we must first understand the concept of a binary tree. A binary is generally the one in which every single node has two further...

3 minutes read.

Breadth First Search

Breadth First Search Breadth first search is a graph traversing algorithm. In this, we start traversing from the source node or any selected node and traverse the graph layer by layer....

6 minutes read.

Stack Using Linked List

In the linked list implementation of the stack, we use a linked list as the primitive data structure to create the stack. It is called the dynamic implementation of the...

6 minutes read.

Function to Delete a Leaf Node from a Binary Tree

Implementation // We are writing a C++ code to eliminate all the leaves from the given value.  #include <bits/stdc++.h> using namespace std; // creating a new binary tree node struct __nod { int record; struct __nod *Lft,...

4 minutes read.

Counting Sort

Counting Sort: Counting sort is a sorting algorithm that is used to sort the elements of the array within a specific range. It counts the same element number of the...

3 minutes read.

Bin Packing Problem (How to minimize the number of used Bins)

You have been given an array. The values of the array represent the size of n different items. You have been also given some bins. You have to store the...

3 minutes read.

Data Structure Prefix to Postfix Conversion

Prefix to Postfix Conversion Prefix: As the name suggests if the operator placed before the operands called the prefix expression.  The form of prefix expression is (operator, operand1, operand2). Example:  *+EF-GH (Infix:...

2 minutes read.

Merge Conflicts and ways to handle them

Merge Conflicts Whenever dealing with the Git merge operations, conflicts will be the frequently occurred. When more than two developers work on the same file on different systems using Git, they...

4 minutes read.

Binary Search

Binary Search: When there is a large data structure, the linear search takes a lot of time to search the element. The binary search was developed to overcome the lack...

7 minutes read.

Doubly Linked List

Doubly Linked List Doubly linked list is another kind of Linked list. Doubly linked list contains two pointers for navigation. In this, we can traverse the list in both directions, either...

4 minutes read.