Analysis of Algorithms | Big | 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 ; Given Input ; Function Call
function linearSearch ( a , n , key ) { for ( var i = 0 ; i < n ; i ++ ) { if ( a [ i ] == key ) return true ; } return false ; } var arr = [ 2 , 3 , 4 , 10 , 40 ] ; var x = 10 ; var n = arr . length ; if ( linearSearch ( arr , n , x ) ) document . write ( " " ) ; else document . write ( " " ) ;