Linear Search in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef struct N { int d; struct N *l, *r; } N;
N* n(int d) { N* t = malloc(sizeof(N)); t->d = d; t->l = t->r = NULL; return t; }
int isBST(N* r, int min, int max) {
    if (!r) return 1;
    if (r->d <= min || r->d >= max) return 0;
    return isBST(r->l, min, r->d) && isBST(r->r, r->d, max);
}
int main() {
    N *r = n(10);
    r->l = n(5); r->r = n(15);
    r->r->l = n(6); // violates BST
    printf("%s", isBST(r, INT_MIN, INT_MAX) ? "Valid" : "Invalid");
}

C Output

Input:  
      10
     /  \
    5    15
         /
        6

Output:  

Invalid



C++ Program

#include <iostream>
#include <climits>
using namespace std;
struct N { int d; N *l, *r; N(int v): d(v), l(0), r(0) {}; };
bool isBST(N* r, int min, int max) {
    if (!r) return true;
    if (r->d <= min || r->d >= max) return false;
    return isBST(r->l, min, r->d) && isBST(r->r, r->d, max);
}
int main() {
    N* r = new N(20);
    r->l = new N(10); r->r = new N(30);
    r->r->l = new N(25);
    cout << (isBST(r, INT_MIN, INT_MAX) ? "Valid" : "Invalid");
}

C++ Output

Input:  
       20
      /  \
    10    30
         /
       25  

Output:  

Valid



JAVA Program

class N {
    int d; N l, r;
    N(int d) { this.d = d; }
}
public class Main {
    static boolean isBST(N r, int min, int max) {
        if (r == null) return true;
        if (r.d <= min || r.d >= max) return false;
        return isBST(r.l, min, r.d) && isBST(r.r, r.d, max);
    }
    public static void main(String[] a) {
        N r = new N(5);
        r.l = new N(3); r.r = new N(8);
        r.l.l = new N(1); r.l.r = new N(4);
        System.out.println(isBST(r, Integer.MIN_VALUE, Integer.MAX_VALUE) ? "Valid" : "Invalid");
    }
}

JAVA Output

Input:  
        5
       / \
      3   8
     / \
    1   4 

Output:  

Valid



Python Program

class N:
    def __init__(s, d): s.d, s.l, s.r = d, None, None
def isBST(r, mn, mx):
    if not r: return True
    if not (mn < r.d < mx): return False
    return isBST(r.l, mn, r.d) and isBST(r.r, r.d, mx)

r = N(10)
r.l = N(5); r.r = N(15)
r.r.l = N(12); r.r.r = N(18)
print("Valid" if isBST(r, float('-inf'), float('inf')) else "Invalid")

Python Output

Input:  
        10
       /  \
      5    15
          /  \
        12    18  

Output:  

Valid



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.