Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort

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



ADVERTISEMENT
ADVERTISEMENT