C Program
#include<stdio.h> int main() { int a[100], n, x, i; scanf("%d%d", &n, &x); for(i = 0; i < n; i++) scanf("%d", &a[i]); for(i = 0; i < n && a[i] != x; i++); printf(i < n ? "Found at %d" : "Not Found", i); }
C Output
Input: 5 12 3 7 12 9 5 Output: Found at 2
C++ Program
#include<iostream> using namespace std; int main() { int a[100], n, x, i; cin >> n >> x; for(i = 0; i < n; i++) cin >> a[i]; for(i = 0; i < n && a[i] != x; i++); cout << (i < n ? "Found at " + to_string(i) : "Not Found"); }
C++ Output
Input: 4 8 1 2 3 8 Output: Found at 3
JAVA Program
import java.util.*; class L { public static void main(String[] a) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), x = sc.nextInt(), i; int[] arr = new int[n]; for(i = 0; i < n; i++) arr[i] = sc.nextInt(); for(i = 0; i < n && arr[i] != x; i++); System.out.print(i < n ? "Found at " + i : "Not Found"); } }
JAVA Output
Input: 6 10 4 5 10 23 42 11 Output: Found at 2
Python Program
n, x = map(int, input().split()) a = list(map(int, input().split())) print(f"Found at {a.index(x)}" if x in a else "Not Found")
Python Output
Input: 5 7 2 4 6 7 9 Output: Found at 3
In-Depth Learning – Entire Concept in Paragraphs
What Is Linear Search?
Linear Search, or sequential search, is among the simplest search algorithms in programming. It tests every element of a list or array individually until it finds the target value or exhausts the list. It doesn't need to sort and can operate on sorted as well as unsorted arrays.
How the Code Works
We first read n (array size), x (target), and the array. We then loop over each element:
If we hit the element equal to x, we exit and return its index.
If we get to the end without finding a match, we return "Not Found".
In C and C++, the for loop gets to use a neat trick: for(i = 0; i < n && a[i] != x; i++); i.e., it gets to break the moment the element is encountered. In Python, we employ the in keyword along with .index() for a clean, readable implementation.
Example
Let's consider an example:
sql
Array: [4, 5, 10, 23, 42, 11]
Search for: 10
We begin from the start:
4 → Not 10
5 → Not 10
10 → Match found at index 2
So we return "Found at 2".
If the value is not in the list, we return "Not Found".
Real-Life Analogy
Picture looking for a name in a guest list where the names are not arranged. You begin from the top and work your way down one at a time. If you encounter the name, you stop. If you reach the bottom and the name is not there, you decide it's not in the list. That's linear search — straightforward, uncomplicated, and easy to understand.
This is how you'd search in real life when the data is not sorted or you don't know where the item could be.
When to Use Linear Search
Use Linear Search when:
The list is unsorted
The list is small
You require a simple and fast solution
You don't want binary search logic or data preparation overhead
It's commonly used in interview warm-up rounds, MCQs, logical reasoning sections, and introductory programming problems.
Time Complexity
Best Case: O(1) → item is at the start
Average Case: O(n/2)
Worst Case: O(n) → item is not found or is the final one
Python-Specific Benefit
Python has a straightforward mechanism for performing linear search through:
python
if x in a:
print(a.index(x))
This does a presence check and returns its location — no loop required! But internally, it still does a linear search.
SEO-Optimized Natural Paragraph for Ranking
Want the simplest method of doing linear search in programming? This guide demonstrates how to carry out a basic linear search in C, C++, Java, and Python using concise and effective code samples. Linear search is a simple, easy technique to use in order to locate a value within an array without needing to sort. Whether you are practicing coding interviews, practising logic puzzles, or learning search algorithms, understanding linear search will give you a solid understanding of data structures and algorithmic thinking. With real-world examples and language-specific optimization advice, this is your one-stop guide to mastering linear search in whatever programming language you are using.
Social Plugin