Linear Search Program using 1-D Array
Linear search method is one of the most taught searching algorithm in any computer class. Here we are implementing binary search method on un-sorted one dimensional array.
Here in this program we are initializing one dimensional array of integer of size 10 and one data member from the keyboard. These values are now passed to a function named as “linear_search” that return “1” on successful search of the element and “0” on unsuccessful search.
// program to search an element from a list of elements using linear search method
// Developed by : rakesh kumar
using namespace std;
int linear_search(int x,int n, int data)
int found =0;
cout<<“Enter x[“<<i+1<<“] Element : “;
cout<<“\n Enter element that you want to search :”;
result = linear_search(x,10,data);
cout<<“Element does not exists”;
cout<<“Element exist in this array”;
Linear search is also known as a sequential search method and this method is the best method to locate any element when your list is not in any sequence.
The binary search method is used when your list is in any sorted order. If you need any such program in C++ then please send your request through comments.