Analysis of Algorithms | Big | C ++ program for the above approach ; Function to find whether a key exists in an array or not using linear search ; Traverse the given array , a [ ] ; Check if a [ i ] is equal to key ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool linearSearch ( int a [ ] , int n , int key ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == key ) return true ; } return false ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int x = 10 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( linearSearch ( arr , n , x ) ) cout << " Element ▁ is ▁ present ▁ in ▁ array " ; else cout << " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ; return 0 ; }