Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Data Structure » Linear search algorithm | Linear search program in C, Example

  • Prithviraj Chauhan Biography in Hindi | पृथ्वीराज चौहान जीवनी हिंदी
    Prithviraj Chauhan Biography in Hindi | पृथ्वीराज चौहान जीवनी हिंदी Biography
  • Lanka Kand Summary in Hindi | Ram Vs Ravana | लंका काण्ड
    Lanka Kand Summary in Hindi | Ram Vs Ravana | लंका काण्ड Spiritual
  • Aranya Kand with Hindi Meaning | अरण्यकाण्ड | सीता हरण
    Aranya Kand with Hindi Meaning | अरण्यकाण्ड का अर्थ | सीता हरण Spiritual
  • Fundamental Duties of Indian Citizens
    Fundamental Duties of Indian Citizens | 11 मौलिक कर्तव्य हिंदी में General Knowledge
  • Ganesh Chaturthi Puja in Hindi | गणेश चतुर्थी का व्रत, महत्व, कथा
    Ganesh Chaturthi Puja in Hindi | गणेश चतुर्थी का व्रत, महत्व, कथा Festival
  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
    Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi Biography
  • Unicef day
    Unicef day is celebrated on December 11 | Speech on unicef day General Knowledge
  • World Environment Day in Hindi : Objective, Importance, Theme
    World Environment Day in Hindi : Objective, Importance, Theme General Knowledge

Linear search algorithm | Linear search program in C, Example

Posted on April 11, 2022April 11, 2022 By GeekCer Education No Comments on Linear search algorithm | Linear search program in C, Example
Linear search algorithm | Linear search program in C, Example

The linear search algorithm is a very simple and straightforward in data structure. A linear search is also known as a sequential search. If the list or array is unordered, the linear search is preferable.

Linear search is an excellent approach to find elements, however we must check each element of the array or list in this search. As a result, the program’s speed slows down in comparison to others.

Table of Contents

  • How does linear search work?
  • Linear search algorithm Steps
  • Pseudocode of Linear search algorithm
  • Linear search program in C using array
  • Different program of Linear search in C
  • Linear search algorithm Time Complexity
  • Linear search algorithm Space Complexity

How does linear search work?

The search begins sequentially, with the leading element being compared to the entries in the list. We check each item in the list and return a flag if a match is found; otherwise, we keep searching until we reach the end of the list or array.

Linear search algorithm Steps

We can cover the full searching with only 6 steps since the steps of the sequential search are so simple.

  • Step 1: Take the first element from the list which will be the current element.
  • Step 2: Make a comparison between the current element and the key element. If both are same, go to step 5.
  • Step 3: If the next element is present in the list, consider it as the current element and got to Step 2.
  • Step 4: If key element not found in list. Go to step 6.
  • Step 5: Key element found then return location or flag.
  • Step 6: Exit of the Algorithm.

Pseudocode of Linear search algorithm

search (array, key)
   for item in the array
      if match item == key then
         return the flag;
      end if
   end for
end of search

Linear search program in C using array


#include <stdio.h>
#define MAX 7

int lrSearch(int arr[], int keyElement) {
  int i = 0;
  for (i = 0; i < MAX; i++) {
    if (arr[i] == keyElement) {
      return i;
    }
  }
  return -1;
}
int main() {
  int arr[] = {
    11,
    21,
    31,
    41,
    51,
    61,
    71
  };
  int keyElement = 31;
  printf("Position of element %d is %d\n", keyElement, (lrSearch(arr, keyElement) + 1));
  return 0;
}


Output:
Position of element 31 is 3

Different program of Linear search in C


#include <stdio.h>

#define MAX 7
void linear_search(int list[], int keyElement) {
  int i, flag = 0;
  for (i = 0; i < MAX; i++)
    if (list[i] == keyElement) {
      printf("\nThe element %d is present at position %d in list\n", keyElement, i);
      flag = 1;
      break;
    }
  if (flag == 0) {
    printf("\nThe element %d is not present in the list\n", keyElement);
  }
}

void printlist(int list[]) {
  int i;
  printf("The elements of the list are: \n");
  for (i = 0; i < MAX; i++) {
    printf("%d\t", list[i]);
  }
}
void main() {
  int list[MAX] = {
    10,
    11,
    12,
    13,
    15,
    16,
    17
  }, n, keyElement = 16;

  printf("\nThe list before sorting is:\n");
  printlist(list);
  linear_search(list, keyElement);
}


Output:
The list before sorting is:
The elements of the list are: 
10      11      12      13      15      16      17
The element 16 is present at position 5 in list

Linear search algorithm Time Complexity

Because the search time is proportional to the number of comparisons required, this will typically do about n/2 comparisons.

  • Worst Case Complexity of Linear Search is O(n).
  • Average Case Complexity of Linear Search is O(n).
  • Best Case Complexity of Linear Search is O(1).

Linear search algorithm Space Complexity

Space Complexity of Linear Search is O(1).

In conclusion, When the number of entries in the list is small, we should use the linear (sequential) search approach.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Data Structure Tags:Algorithm for linear search in c, What is the time complexity of linear search?

Post navigation

Previous Post: Binary search algorithm in C, Recursive, Iterative, examples
Next Post: Linked List in Data Structure and Algorithm, Types, Operations

More Related Articles

Binary search in Data structures In C (Recursive and Iterative) Binary search algorithm in C, Recursive, Iterative, examples Data Structure
Linked List in Data Structure and Algorithm, Types, Operations Linked List in Data Structure and Algorithm, Types, Operations C Language
Bubble Sort Algorithm in Data Structure | Bubble Sort Example Bubble Sort Algorithm in Data Structure | Bubble Sort Example Data Structure
Dynamic memory allocation in C, malloc, realloc, calloc and free Dynamic memory allocation in C using malloc, realloc, calloc and free Data Structure

Leave a Reply Cancel reply

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

  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved