Simple Linear Search Example Program in C++
Definition
- Linear search is also called sequential search
- Linear search is a method for searching a value within an array.
- It sequentially checks one by one of the arrays for the target element until a match is found or until all the elements have been searched of that array.
Why is the linear search also called sequential search
- Linear search sequentially checks one by one of the collection(from 0th position to end) for the search element. So It is called sequential search.
Simple Linear Search Example Program (Sequential search)
/* Simple Linear Search Program in C++*/
/* Data Structure C++ Programs,C++ Array Examples */
#include <iostream>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 5
using namespace std;
int main() {
int arr_search[MAX_SIZE], i, element;
cout << "Simple C++ Linear Search Example - Array\n";
cout << "\nEnter " << MAX_SIZE << " Elements for Searching : " << endl;
for (i = 0; i < MAX_SIZE; i++)
cin >> arr_search[i];
cout << "\nYour Data :";
for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << arr_search[i];
}
cout << "\nEnter Element to Search : ";
cin>>element;
/* for : Check elements one by one - Linear */
for (i = 0; i < MAX_SIZE; i++) {
/* If for Check element found or not */
if (arr_search[i] == element) {
cout << "\nLinear Search : Element : " << element << " : Found : Position : " << i + 1 << ".\n";
break;
}
}
if (i == MAX_SIZE)
cout << "\nSearch Element : " << element << " : Not Found \n";
getch();
}
Sample Output
Simple Linear Search Example - Array
Enter 5 Elements for Searching :
500
400
300
200
111
Enter Element to Search : 200
Linear Search : 200 is Found at array : 4.
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor Overloading In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Example Program For Constructor In C++
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Exception Handling Divide by zero Using C++ Programming