• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C » Linear Search in C
Next →
← Prev

What is linear search in C language?

By Dinesh Thakur

In this tutorial, we will understand the concept of “Linear search in C“, we’ll write a c program for linear search that searches for an element in an array using a Linear Search Algorithm. Before we proceed throughout the program, let’s see what is meant by linear search, advantages and disadvantage of linear search. We’ll talk about more linear search and then code a program in C language.

Definition: Linear search, also called as orderly search or sequential search, because each crucial element is searched from the first element in an array, i.e. a[0] to final element in an array, i.e. a[n-1]. It assesses each element of the list without jumping before a match is found or the entire list was searched.

Note: Linear search is called linear as it goes through every element in its search.

We’ll be covering the following topics in this tutorial:

  • What is meant by linear search in C?
  • Time complexity of linear search
  • Linear search is implemented using following approaches
  • Pseudocode for Linear Search
  • Implementing linear search program in c
  • Why linear search is not efficient
  • Difference between linear search and binary search
  • Advantages of a linear search
  • Disadvantages of a linear search

What is meant by linear search in C?

We are aware that arrays are stored in memory in a linear manner, which means successive elements are stored alongside each other. So if we wish to search a thing from the array, the algorithm begins from an initial element and compares it with our essential item, then goes on next successive element till primary element is found or list endings.

For example, Consider an array of integers. You need to find and print the position of all of the elements with value. The linear search is based on matching notion, every element from the start to the end of the list, and then printing the position of the element when the condition is ‘True’.

Consider it as a means of finding your way at a phonebook. A Linear Search is beginning at the start, studying every name until you find everything you’re searching.

linear search in cTime complexity of linear search

The time complexity of linear search is O(log n) because every element in an array is compared only once.

• Best case: O(1) – When an element is located at the initial position (it requires only one check).
• Worst Case: O(n) – If an element isn’t found in the list. Then the function will check all of the n elements.
• Typical Case: O(n) – requires an n/2 check.

Linear search is implemented using following approaches

• Begin from the first element of array list and one by one compare the element we’re searching for with each element of the array.
• When there’s a match involving the element we’re searching for the element of the array, return the index.
• When there’s absolutely no match between the element we’re searching for the element of the array, return -1.

Pseudocode for Linear Search

procedure linear_search (list, value)
 for each item in the list
   if match item == value
     return the item's location
   end if
 end for
end procedure

Implementing linear search program in c

#include<stdio.h>
#include<conio.h>
void main(){
  int list[20],size,i,sElement;
  printf("Enter the required list size: ");
  scanf("%d",&size);
  printf("Enter any %d numbers: ",size);
  for(i = 0; i < size; i++)
    scanf("%d",&list[i]);
    printf("Enter the element you want to Search: ");
    scanf("%d",&sElement);
    // Linear Search Logic
    for(i = 0; i < size; i++) {
      if(sElement == list[i]) {
        printf("Searched Element is found at %d index", i);
        break;
      }
   }
   if(i == size)
     printf("Searched element is not found in the list!!!");
     getch();
}

Output:

c program for linear searchThe time necessary to search an element with a linear search algorithm is dependent upon the size of this list. From the best-case situation, the element is present at the start of the list and the worst-case, it’s present at the conclusion.

The linear search time complexity is O(n), which makes it generally not as effective than binary search (O(log n)). However, while list items could be ordered from greatest to least and the probabilities seem as geometric distribution (f (x)=(1-p) x-1p, x=1,2). The linear search may possess the capability to be significantly faster than a binary search.

Why linear search is not efficient

There’s not any doubt that linear search is straightforward. But because it compares each element one by one, it’s time-intensive and therefore not so efficient. If we must locate a number out of, 1,000 and number is in the last place, then a linear search algorithm could turn out quite tedious.

Difference between linear search and binary search

• Binary Search necessitates the input information to be sorted; Linear Search does not.
• Binary Search necessitates an ordering contrast; Linear Search needs equality comparisons.
• Binary Search has complexity O(log n); Linear search has complexity O(n) as mentioned previously.
• Binary Search requires random access to this information; Linear Search needs sequential access (that can be quite significant — it means that a Linear Search can flow data of random size).

Advantages of a linear search

• When a key element matches the first element in the array, then linear search algorithm is best case because executing time of linear search algorithm is 0 (n), where n is the number of elements in an array.
• The list doesn’t have to sort. Contrary to a binary search, linear searching does not demand a structured list.
• Not influenced by insertions and deletions. Since the linear search doesn’t call for the list to be sorted, added elements can be inserted and deleted. As with other searching, algorithms might need to reorder the list after insertions or deletions. It might sometimes indicate a linear search will probably be more effective.
• An benefit of a linear search on a binary search is that the information has to be in sorted order to get a binary search.

Disadvantages of a linear search

• The drawback of a linear search is the fact that its time consuming for the enormous arrays.
• Inversely, slow searching of big lists. Every time a vital element matches the last element from the array or an essential element does not match any element Linear search algorithm is the worst case.

You’ll also like:

  1. What is Linear Search
  2. Array C++ Linear Search
  3. Linear Search in Python
  4. Difference Between Procedure Oriented Language and Object Oriented Language
  5. Binary Search in C
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C Programming

C Programming Tutorials

  • C - History
  • C - Anatomy
  • C - Constants
  • C - Identifiers
  • C - Data Types
  • C - Libraries File
  • C - Header Files
  • C - Basic Language
  • C - Data Types Sizes
  • C - Header Files Importance
  • C - Escape Sequences
  • C - Main() Purpose
  • C - Program Procedure
  • C - Control Statements
  • C - Enumeration Constant
  • C - Add numbers
  • C - Return Statement
  • C - Avoid Goto
  • C - Command Line Arguments
  • C - Switch Case
  • C - Switch Case Limitations
  • C - getchar() and putchar()
  • C - Iteration Statements
  • C - Pass by Value and Reference
  • C - Structures and Unions
  • C - Structure
  • C - Dynamic Memory
  • C - Fgets and Fputs Functions
  • C - Gets() and Puts() Functions
  • C - Armstrong Number
  • C - Storage Classes
  • C - Fibonacci Series
  • C - Precision Setting
  • C - const Parameters

C - Variable & It's Type

  • C - Variables
  • C - Variable Lifetime
  • C - Static Variable
  • C - Register Variable
  • C - Global Variables
  • C - Auto Variables
  • C - Local Variables

C - Operator & Expressions

  • C - Operator
  • C - Boolean Operators
  • C - Bitwise Operator
  • C - Arithmetic Operators
  • C - Modulus Operator
  • C - Ternary Operator
  • C - Expressions
  • C - Arithmetic Expressions

C - Array

  • C - Arrays
  • C - Array Types
  • C - Array Characteristics
  • C - Static Arrays
  • C - Global Arrays
  • C - 3D Arrays
  • C - Dynamic Arrays
  • C - Pointer to 3D Arrays
  • C - Array Elements Hold
  • C - Arrays as Function Parameters
  • C - Accessing Matrix Elements
  • C - File Handling
  • C - Matrix Multiplication
  • C - Dynamic Memory Allocation

C - Searching & Sorting

  • C - Data Structures
  • C - Linear Search
  • C - Bubble Sort
  • C - Merge Sort
  • C - Linked List
  • C - Insertion Sort
  • C - Binary Search
  • C - Selection Sort
  • C - Quick Sort

C - Functions

  • C - Functions
  • C - Functions Advantages
  • C - Void Functions
  • C - Function Call
  • C - Default Return Value
  • C - String functions

C - Pointer

  • C - Pointers
  • C - Type Casting Of Pointers
  • C - Pointer Advantages
  • C - Pointers Initialization
  • C - Vectors and Pointers

C - Differences

  • C - C Vs C++
  • C - Formal Args. Vs Actual Args.
  • C - Keywords Vs Identifiers
  • C - Strings Vs Character Arrays
  • C - Address Vs Dereference Operator
  • C - Goto Vs longjmp
  • C - Declaring Vs Defining Variable
  • C - String Vs Array
  • C - Call by Value Vs Reference
  • C - Structure Vs Union
  • C - For Vs While loops
  • C - Compiler Vs Interpreter

C - Programs

  • C Program Standard Deviation
  • C Program Calculate Tax
  • C Program Sum Series
  • C Program Merge Arrays
  • C Program Euclid’s Algorithm
  • C Program Print Weekdays
  • C Program Sum of Digits
  • C Program Print a list
  • C Program Print Pythagorean
  • C Program Quiz program
  • C Program Display Table
  • C Program Print Comma-Separated
  • C Program Prints Prime Numbers
  • C Program for Print Integer
  • C Program Count Number
  • C Program Print Color Name
  • C Program Print Odd Numbers
  • C Program Calculate area
  • C Program for a Menu
  • C Program Add Two Vectors
  • C Program Array Addresses
  • C Program Division by Zero Error
  • C Program Compare two Dates
  • C Program Tower of Hanoi
  • C Program return 3 Numbers
  • C Program for Prime Numbers
  • C Program for Factorial
  • C Program for Palindrome

Other Links

  • C Programming - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW