linear search c++ program code example
Example 1: linear search in c++
bool linearsearch(int A[], int N, int X) {
for (int i=0; i<N; i++)
if (A[i] == X) return true;
return false;
}
Example 2: linear search
def global_linear_search(target, array)
counter = 0
results = []
while counter < array.length
if array[counter] == target
results << counter
counter += 1
else
counter += 1
end
end
if results.empty?
return nil
else
return results
end
end