# Linear Search

Linear Search means to traverse a given list or array one by one sequencially starting from the first element with the element to be searched until a match is found or end of the array is reached.

so in the above figure element to be searched is 5 and it is found at index 6.

if the match is found index of the matched element is retured else -1 is returned

`Input : arr[] = {11,8,34,2,3,76}`

N=6,key = 3;

Output : 4

Element 3 is present at index 4

Input : arr[] = {10, 20, 30, 60, 50, 110, 100}

N=7,key = 175;

Output : -1

Element 175 is not present in arr[].

implementation:

**int search(int arr[], int N, int key)
{
// Start traversing the array
for (int i = 0; i < N; i++)
{
// If a successful match is found,
// output the index
if (arr[i] == key)
return i;
}
// If the element is not found,
// and end of array is reached
return -1;
} **

Time Complexity: O(N)

lets implement the same code using cout in c++:

int search(int arr[], int N, int key) { int p=0; // Start traversing the array for (int i = 0; i < N; i++) { // if the some array element is equal to element to be searched if (arr[i] == key) { cout<< i; p++; break; } }// if p=0 than element to be searched is found// If the element is not found, // and end of array is reached if(p==1) cout<< -1; }