Linear Search
Linear Search Algorithm in C++ (Complete Implementation)
C++ Linear Search Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i; // Return index if found
}
}
return -1; // Return -1 if not found
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
int key;
cout << "Array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
cout << "Enter element to search: ";
cin >> key;
int result = linearSearch(arr, n, key);
if (result != -1) {
cout << "Element found at index: " << result << endl;
} else {
cout << "Element not found in array" << endl;
}
return 0;
}Array: 64 34 25 12 22 11 90 Enter element to search: 25 Element found at index: 2
Understanding Linear Search
This program teaches you how to implement the Linear Search algorithm in C++. Linear Search is the simplest searching algorithm that sequentially checks each element of the array from the beginning until it finds the target element or reaches the end. It works on both sorted and unsorted arrays, making it versatile but inefficient for large datasets.
---
1. What This Program Does
The program searches for an element in an array using Linear Search. For example:
Linear Search checks each element one by one until it finds a match or exhausts all elements.
---
2. Header File Used
This header provides:
---
#include <iostream>3. Understanding Linear Search
Algorithm Concept
:
Visual Example
:
---
4. Function: linearSearch()
int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
}
}
return -1; // Return -1 if not found
}
return i; // Return index if foundHow it works
:
---
5. Step-by-Step Algorithm
For each element in array
:
Step 1: Check Current Element
Step 2: Move to Next Element
Step 3: End of Array
Example
(searching for 25):
---
6. Time and Space Complexity
Time Complexity
:
Space Complexity
: O(1)
---
7. When to Use Linear Search
Best For
:
Not Recommended For
:
---
8. Important Considerations
Works on Any Array
:
Return Value
:
Comparison Count
:
---
9. Advantages and Disadvantages
Advantages
:
Disadvantages
:
---
10. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning searching algorithms, understanding sequential access, and preparing for more efficient algorithms like Binary Search in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Linear Search
This C++ program is part of the "Sorting & Searching Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.