Searches for an element in an array sequentially until a match if found or the whole array has been searched.
Best Case: Ω(1)
Average Case: θ(n)
Worst Case: O(n)
where n represents the size of the array.
Worst Case: O(1)
Given a key, it checks if the key is present in the array. The function linear_search
has the following parameter(s):
key
: the integer to searcha
: a vector of integers
Return value: true if the element was found, else false.
The first line contains two integers n
and key
, where n
is the size of array and key
is the value to search.
The next line contains n
space-separated integers a[i]
where 0 ≤ i
< n
.
1
or true if the element was found else 0
or false.
5 4
1 2 3 4 5
1
5 7
1 2 3 4 5
0